如何以编程方式将按钮上的可见性绑定到C#中的INotifyPropertyChanged布尔属性?

时间:2011-08-24 19:45:56

标签: c# wpf

这是我到目前为止所得到的:

System.Windows.Data.Binding binding = new System.Windows.Data.Binding("MyProperty");
binding.Mode = System.Windows.Data.BindingMode.TwoWay;
binding.Converter = new System.Windows.Controls.BooleanToVisibilityConverter(); 
binding.Source = mySourceObject;

this.SetBinding(this.myButton.Visibility, binding);

但是可见性不是依赖属性,所以我该怎么做呢?

2 个答案:

答案 0 :(得分:11)

你应该能够做到

Button.VisibilityProperty

而不是

this.myButton.Visibility

http://msdn.microsoft.com/en-us/library/system.windows.uielement.visibilityproperty.aspx

答案 1 :(得分:6)

您需要传入静态Visibility依赖项属性本身(Button继承自UIElement),而不是按钮实例上的属性值,例如:

myButton.SetBinding(UIElement.VisibilityProperty, binding);