我正在尝试绑定网格的可见性,但无法这样做。
//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>
在调试时,程序将值设置为隐藏,但是当我更改_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;
错误!在RaisePropertyChanged(&#34; isVisiblePane&#34;)行。好像没有这个名字的财产 &#34;类型&#39; System.ArgumentException&#39;的例外;发生在GalaSoft.MvvmLight.dll但未在用户代码&#34;
中处理PS:我也尝试过bool的IValueConverter方法。仍然没有弄清楚问题是什么。有什么帮助吗?
答案 0 :(得分:3)
没有真正回答,但是:
isVisiblePane
更改为IsPaneVisible
。答案 1 :(得分:3)
如果您的视图也是您的UI,请确保最终在正确的线程上下文中调用PropertyChanged的RaisePropertyChanged。
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
确保网格拥有您的VM作为其数据上下文,或指定适当的绑定来源。
确保RaisePropertyChanged通过
绑定了任何内容if(RaisePropertyChanged!= null) RaisePropertyChanged(....)
答案 2 :(得分:0)
Very late but everyone keeps suggestions to use a converter. Good suggestion but still requires the use of more code behind. You can use a data trigger as part of the style for your UI element and change visibility based on value of the bool in your model.
即
<Grid>
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=MyBoolValue}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>