【问题标题】:INotifyPropertyChanged doesn`t update UI(WPF)INotifyPropertyChanged 不更新 UI(WPF)
【发布时间】:2018-04-08 20:07:28
【问题描述】:

问题在于 ViewModel 属性与控件属性的绑定无法正常工作。我检查了属性及其值的变化,但控件的可见性没有改变。知道这涉及到什么吗?还是我错过了什么?

视图模型:

class MainViewModel
{
    public LoginViewModel LoginViewModel { get; set; }
    Notifier notifier = new Notifier();

    public MainViewModel()
    {
        LoginViewModel = new LoginViewModel();
    }
    private Visibility mdiPanelVisibility=Visibility.Visible;
    public Visibility MDIPanelVisibility
    {
        get
        {
            return mdiPanelVisibility;
        }
        set
        {
            mdiPanelVisibility = value;
            NotifyPropertyChanged("MDIPanelVisibility");
        }
    }

    private RelayCommand showMDIPanelCommand;
    public RelayCommand ShowMDIPanelCommand
    {
        get
        {
            return showMDIPanelCommand ??
                (showMDIPanelCommand = new RelayCommand(obj =>
                {
                    MDIPanelVisibility = Visibility.Visible;
                }));
        }
    }

    private RelayCommand hideMDIPanelCommand;
    public RelayCommand HideMDIPanelCommand
    {
        get
        {
            return hideMDIPanelCommand ??
                (hideMDIPanelCommand = new RelayCommand(obj =>
                {
                    MDIPanelVisibility = Visibility.Hidden;
                }));
        }
    }
    private event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

和视图:

<Border Visibility="{Binding MDIPanelVisibility}">
        <Border.InputBindings>
            <MouseBinding MouseAction="LeftClick" Command="{Binding HideMDIPanelCommand}"/>
        </Border.InputBindings>
    </Border>
    <ContentPresenter Width="Auto" Grid.RowSpan="2" Panel.ZIndex="1" VerticalAlignment="Center" Visibility="{Binding MDIPanelVisibility}">
        <ContentPresenter.Content>
            <local:MDIView/>
        </ContentPresenter.Content>
    </ContentPresenter>
    <Button Content="Личный кабинет" FontSize="13" Command="{Binding ShowMDIPanelCommand}">
        <Button.Style>
            <Style TargetType="Button" BasedOn="{StaticResource aLogButton}"/>
        </Button.Style>
    </Button> 

【问题讨论】:

    标签: c# wpf mvvm inotifypropertychanged


    【解决方案1】:

    MainViewModel 类需要从 INotifyPropertyChanged 继承,而您的类没有这样做,以便当视图的 DataContext 设置为 MainViewModel 实例时绑定框架能够按预期运行。

    更新类定义

    public class MainViewModel: INotifyPropertyChanged {
        //...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 2011-03-05
      • 1970-01-01
      相关资源
      最近更新 更多