我试图绑定网格的可见性,但无法这样做。
//ViewModel Class
private Visibility _isVisiblePane = Visibility.Hidden;
public Visibility isVisiblePane {
get
{
return _isVisiblePane;
}
set
{
_isVisiblePane = value;
RaisePropertyChanged(() => "isVisiblePane");
}
}
//xaml code
<Grid Visibility="{Binding Path=isVisiblePane}">
....My Content....
</Grid>
Run Code Online (Sandbox Code Playgroud)
在调试时,程序将值设置为隐藏,但是当我更改 _isVisiblePane 的可见性时,它不会更新 GUI 中的可见性(网格保持隐藏状态,而 _isVisiblePane 值是可见的)。
//in some function => on button click, value of _isVisiblePane updates to Visible but grid remains hidden.
isVisiblePane = isLastActiveDoc() == true ? Visibility.Visible : Visibility.Hidden;
Run Code Online (Sandbox Code Playgroud)
错误!在 RaisePropertyChanged("isVisiblePane") 行上。似乎没有具有此名称的属性“GalaSoft.MvvmLight.dll 中发生类型为‘System.ArgumentException’的异常,但未在用户代码中处理”
PS:我也尝试过使用 bool 的 IValueConverter 方法。仍然不知道问题出在哪里。有什么帮助吗?
如果您的视图也是您的 UI,请确保 RaisePropertyChanged 最终在正确的线程上下文中调用 PropertyChanged。
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
Run Code Online (Sandbox Code Playgroud)确保网格将您的虚拟机作为其数据上下文,或者为您的绑定指定正确的源。
确保 RaisePropertyChanged 通过以下方式绑定了任何内容
if(RaisePropertyChanged != null) RaisePropertyChanged (....)