簡體   English   中英

在后面的代碼中綁定靜態屬性

[英]Binding static property in code behind

我正在嘗試閱讀本文 ,唯一的區別是我在后面的代碼中創建並綁定了控件。 不幸的是,它不起作用。 這是我的示例代碼:

 public partial class ShellWindow
 {
      private static Visibility progressbarVisibility = Visibility.Collapsed;
      public static Visibility ProgressbarVisibility
      {
           get { return progressbarVisibility; }
           set
           {
               if (progressbarVisibility == value) return;
               progressbarVisibility = value;
               RaiseStaticPropertyChanged("ProgressbarVisibility");
           }
      }
      public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
      public static void RaiseStaticPropertyChanged(string propName)
      {
           EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
           if (handler != null)
               handler(null, new PropertyChangedEventArgs(propName));
     }
}

我像這樣綁定

var binding = new Binding("ShellWindow.ProgressbarVisibility") { Mode = BindingMode.TwoWay };
       progressbar = new CircularProgressBar ();
  progressbar.SetBinding(VisibilityProperty,
                             binding);

我想我缺少了一些東西,但是我不確定我哪里錯了。 任何幫助都會很棒。

文章說使用:

{Binding (local:Repository.Color)}

由於local:在XAML文件之外沒有任何意義,因此我認為不可能用字符串構造綁定。

您還可以將PropertyPath分配給Binding.Path屬性,並且此PropertyPath構造函數接受PropertyInfo 要使用此構造函數,需要以標記化的格式指定路徑字符串( 此處描述)。 從而:

var propertyInfo = typeof(ShellWindow).GetProperty("ProgressbarVisibility");
var propertyPath = new PropertyPath("(0)", propertyInfo);
var binding = new Binding() { Path = propertyPath, Mode = BindingMode.TwoWay };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM