INotifyPropertyChanged 不更新 UI(WPF)
INotifyPropertyChanged doesn`t update UI(WPF)
问题是 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>
MainViewModel
class 需要从 INotifyPropertyChanged
继承,而您的 class 不需要,以便绑定框架在视图 DataContext
设置为 MainViewModel
实例。
更新 class 定义
public class MainViewModel: INotifyPropertyChanged {
//...
}
问题是 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>
MainViewModel
class 需要从 INotifyPropertyChanged
继承,而您的 class 不需要,以便绑定框架在视图 DataContext
设置为 MainViewModel
实例。
更新 class 定义
public class MainViewModel: INotifyPropertyChanged {
//...
}