在后面的代码中绑定静态属性 [英] Binding static property in code behind

查看:38
本文介绍了在后面的代码中绑定静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循这个文章,唯一的区别是我在后面的代码中创建和绑定控件.不幸的是它不起作用.这是我的示例代码:

I am trying to follow this article, the only difference is that I am creating and binding the control in code behind. Unfortunately it's not working. Here is my sample code :

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

我想我遗漏了一些东西,但我不确定我错在哪里.任何帮助都会很棒.

I think I am missing something, but I am not sure where I am wrong. Any help would be great.

推荐答案

文章说使用:

{Binding (local:Repository.Color)}

由于 local: 在 XAML 文件之外没有任何意义,我认为不可能用字符串构造绑定.

Since local: has no meaning outside a XAML file, I don't think it's possible to construct a binding with a string.

您还可以分配 PropertyPathBinding.Path 属性和 这个 PropertyPath 构造函数 接受 PropertyInfo.要使用此构造函数,需要以标记化格式指定路径字符串(描述 此处).因此:

You can also assign a PropertyPath to the Binding.Path property, and this PropertyPath constructor accepts PropertyInfo. To use this constructor, the path string needs to be specified in tokenized format (described here). Thus:

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

这篇关于在后面的代码中绑定静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆