Home > Enterprise >  Wpf 4.8 binding to static property
Wpf 4.8 binding to static property

Time:06-23

I create viewbox programmaticaly. How to bind this control programaticaly to static property of non static class.

var bindingHeight = new Binding("viewbox_height");
        bindingHeight.Source = Config.viewbox_height;
        bindingHeight.Mode = BindingMode.TwoWay;
        bindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        MyViewbox.SetBinding(Viewbox.HeightProperty, bindingHeight);


public class Config
{
    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
    public static void OnPropertyChanged([CallerMemberName] string propertyname = null)
    {   
   StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyname));
    }

This way doesnt work.

CodePudding user response:

At least you made a mistake in setting Binding.Source. In the case of usual instance property, it should be the object which has that property, in your case, the instance of "Config". In the case of static property, you don't need to set Binding.Source.

CodePudding user response:

Set the source to an instance of a Config:

var bindingHeight = new Binding("viewbox_height");
bindingHeight.Source = new Config();
bindingHeight.Mode = BindingMode.TwoWay;
bindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
MyViewbox.SetBinding(Viewbox.HeightProperty, bindingHeight);
  • Related