简体   繁体   中英

INotifyPropertyChanged doesn`t update UI(WPF)

The problem is that the binding of ViewModel properties to the control`s properties does not work correctly. I checked the properties and their values ​​change, but the visibility of the controls does not change. Any idea what is involved in this? Or am I missing something?

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));
        }
    }
}

and View:

<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> 

The MainViewModel class needs to inherit from INotifyPropertyChanged , which your class does not, in order for the binding framework to behave as expected when the view's DataContext is set to the MainViewModel instance.

Update class definition

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM