C# 在代码隐藏中绑定静态属性
我试图遵循这一点,唯一的区别是我在代码隐藏中创建和绑定控件。 不幸的是,它不起作用。以下是我的示例代码:C# 在代码隐藏中绑定静态属性,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我试图遵循这一点,唯一的区别是我在代码隐藏中创建和绑定控件。 不幸的是,它不起作用。以下是我的示例代码: public partial class ShellWindow { private static Visibility progressbarVisibility = Visibility.Collapsed; public static Visibility ProgressbarVisibility { get { retu
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));
}
}
我想我遗漏了什么,但我不确定我错在哪里。
任何帮助都会很好。文章说要使用:
{Binding (local:Repository.Color)}
由于local:
在XAML文件之外没有任何意义,因此我认为不可能用字符串构造绑定
您还可以为Binding.Path
属性分配一个属性,并接受PropertyInfo
。要使用此构造函数,需要以标记化格式指定路径字符串(如上所述)。因此:
文章说使用:
{Binding (local:Repository.Color)}
由于local:
在XAML文件之外没有任何意义,因此我认为不可能用字符串构造绑定
您还可以为Binding.Path
属性分配一个属性,并接受PropertyInfo
。要使用此构造函数,需要以标记化格式指定路径字符串(如上所述)。因此:
var propertyInfo = typeof(ShellWindow).GetProperty("ProgressbarVisibility");
var propertyPath = new PropertyPath("(0)", propertyInfo);
var binding = new Binding() { Path = propertyPath, Mode = BindingMode.TwoWay };