[英]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.