在XAML绑定能见度到Visibility属性 [英] Binding Visibility in XAML to a Visibility property

查看:850
本文介绍了在XAML绑定能见度到Visibility属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了互联网的一个布尔值绑定到XAML控件的Visibility属性相当的几个例子上。大多数的很好的例子使用BooleanToVisibiliy转换器。

I've seen on the internet quite a few examples of binding a boolean to the Visibility property of a control in XAML. Most of the good examples use a BooleanToVisibiliy converter.

我想只设置控件的Visible属性绑定到code-后面System.Windows.Visibility属性,但它似乎并不想工作。

I'd like to just set the Visible property on the control to bind to a System.Windows.Visibility property in the code-behind, but it doesn't seem to want to work.

这是我的XAML:

<Grid x:Name="actions" Visibility="{Binding Path=ActionsVisible, UpdateSourceTrigger=PropertyChanged}" />

这是code的属性:

private Visibility _actionsVisible;
public Visibility ActionsVisible
{
   get
   {
      return _actionsVisible;
   }
   set
   {
      _actionsVisible = value;
   }
}

在窗口的构造函数中,我也有这个调用:

In the constructor of the Window, I also have this call:

base.DataContext = this;

当我更新要么ActionsVisible或this.actions.Visibility,国家不转让。任何想法可能什么错了?

When I update either ActionsVisible or this.actions.Visibility, the state doesn't transfer. Any ideas to what might be going wrong?

推荐答案

我认为这个问题是WPF不知道你ActionsVisible属性已改变,因为你还没有通知的事实。

I think the problem is that WPF can't know that your ActionsVisible property has changed since you've not notified the fact.

您类需要实现INotifyPropertyChanged,然后在你的ActionsVisible set方法,你需要用ActionsVisible作为已更改的属性名称火PropertyChanged事件。

Your class will need to implement INotifyPropertyChanged, then in your set method for ActionsVisible you'll need to fire the PropertyChanged event with ActionsVisible as the property name that has changed.

希望这有助于...

这篇关于在XAML绑定能见度到Visibility属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆