如何在 WPF 中使用绑定更改可见性
How to change visibility using binding in WPF
我正在使用 MVVM Light WPF 4。
我的 Home.xaml 中有一个 ContentPresenter。
<ContentPresenter Name="MDI" Content="{Binding WindowName, Mode=OneWay}">
我在视图模型中将用户控件绑定到此
public UserControl WindowName { get; set; }
void ShowSalesEntry()
{
WindowName = null;
WindowName = new SalesEntry();
RaisePropertyChanged("WindowName");
}
通过在菜单中使用命令点击,绑定正常。
现在在用户控件中我有一个按钮,我曾经关闭它(但关闭我改变了可见性
以这种方式崩溃)..
Visibility="{Binding visibility, Mode=OneWay}"
在用户控件视图模型中,
public SalesEntryViewModel()
{
visibility = Visibility.Visible;
cmdExitWindow = new RelayCommand(ExitWindow);
RaisePropertyChanged("visibility");
}
和以下关闭(可见性折叠)
public RelayCommand cmdExitWindow { get; set; }
void ExitWindow()
{
visibility = Visibility.Hidden;
RaisePropertyChanged("visibility");
}
退出(意味着可见性崩溃)..
到此为止一切正常。
问题是当我点击同一个页面时,我的意思是显示同一个用户控件,
现在这个时候能见度还是崩溃了。即使我在
加载事件。
如何解决这个..
我是 MVVM WPF 的新手..请帮助我..
Problem is when i click the same page i mean to show the same user
control, now this time the visibility is still collapsed. Even though
i changed to visible in the load event.
根据此评论和提供的代码,您要么省略了代码,要么混淆了构造函数的用途。
在您的构造函数中,您已将可见性设置为 Visible
。然后,您有一个将可见性设置为 Hidden
的方法,但一旦发生这种情况,就无法将其设置回 Visible
。构造函数仅在创建对象时触发。您需要在适当的时间设置可见性(即您的评论“当我单击同一页面时”)。
//Add these lines to the method/event that will show the control again
visibility = Visibility.Visible;
RaisePropertyChanged("visibility");
根据您提供的内容,这是我能给出的最佳答案。
我正在使用 MVVM Light WPF 4。
我的 Home.xaml 中有一个 ContentPresenter。
<ContentPresenter Name="MDI" Content="{Binding WindowName, Mode=OneWay}">
我在视图模型中将用户控件绑定到此
public UserControl WindowName { get; set; }
void ShowSalesEntry()
{
WindowName = null;
WindowName = new SalesEntry();
RaisePropertyChanged("WindowName");
}
通过在菜单中使用命令点击,绑定正常。
现在在用户控件中我有一个按钮,我曾经关闭它(但关闭我改变了可见性 以这种方式崩溃)..
Visibility="{Binding visibility, Mode=OneWay}"
在用户控件视图模型中,
public SalesEntryViewModel()
{
visibility = Visibility.Visible;
cmdExitWindow = new RelayCommand(ExitWindow);
RaisePropertyChanged("visibility");
}
和以下关闭(可见性折叠)
public RelayCommand cmdExitWindow { get; set; }
void ExitWindow()
{
visibility = Visibility.Hidden;
RaisePropertyChanged("visibility");
}
退出(意味着可见性崩溃).. 到此为止一切正常。
问题是当我点击同一个页面时,我的意思是显示同一个用户控件, 现在这个时候能见度还是崩溃了。即使我在 加载事件。
如何解决这个.. 我是 MVVM WPF 的新手..请帮助我..
Problem is when i click the same page i mean to show the same user control, now this time the visibility is still collapsed. Even though i changed to visible in the load event.
根据此评论和提供的代码,您要么省略了代码,要么混淆了构造函数的用途。
在您的构造函数中,您已将可见性设置为 Visible
。然后,您有一个将可见性设置为 Hidden
的方法,但一旦发生这种情况,就无法将其设置回 Visible
。构造函数仅在创建对象时触发。您需要在适当的时间设置可见性(即您的评论“当我单击同一页面时”)。
//Add these lines to the method/event that will show the control again
visibility = Visibility.Visible;
RaisePropertyChanged("visibility");
根据您提供的内容,这是我能给出的最佳答案。