Wpf 4.8绑定到静态属性



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

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

这个方法行不通。

设置源为Config的实例:

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

至少你在设置Binding.Source时犯了一个错误。在通常的实例属性中,它应该是具有该属性的对象,在你的例子中,是"Config"的实例。如果是静态属性,则不需要设置Binding.Source

相关内容

  • 没有找到相关文章