问题是 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
类需要从 INotifyPropertyChanged
继承,而您的类不需要,以便当视图的DataContext
设置为MainViewModel
实例时,绑定框架的行为符合预期。
更新类定义
public class MainViewModel: INotifyPropertyChanged {
//...
}