繁体   English   中英

Wpf 4.8 绑定到静态属性

[英]Wpf 4.8 binding to static property

我以编程方式创建视图框。 如何以编程方式将此控件绑定到非静态类的静态属性。

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

这种方式行不通。

至少你在设置Binding.Source时犯了一个错误。 在通常的实例属性的情况下,它应该是具有该属性的对象,在您的情况下,是“Config”的实例。 在静态属性的情况下,您不需要设置Binding.Source

将源设置为Config的实例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM