How to programmatically bind visibility on a button to INotifyPropertyChanged boolean property in C #?

Here is what I got so far:

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); 

but visibility is not a dependency property, so how can I do this?

+6
source share
2 answers

You should be able to do

 Button.VisibilityProperty 

instead

 this.myButton.Visibility 

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

+10
source

You need to pass the static visibility dependency property (which Button inherits from UIElement ), and not the value of the property on the button instance, for example:

 myButton.SetBinding(UIElement.VisibilityProperty, binding); 
+5
source

Source: https://habr.com/ru/post/895799/


All Articles