C# 如何以编程方式绑定到静态属性?

C# 如何以编程方式绑定到静态属性?,c#,.net,wpf,xaml,data-binding,C#,.net,Wpf,Xaml,Data Binding,如何以编程方式绑定到静态属性?我可以用C#来做什么 更新:是否可以执行单向源绑定?我知道双向是不可能的,因为静态对象上没有更新事件(至少在.NET4中是这样)。我无法实例化对象,因为它是静态的。单向绑定 假设您有一个带有静态属性Name的类Country public class Country { public static string Name { get; set; } } 现在您需要将属性Name绑定到TextBlock的TextProperty Binding binding

如何以编程方式绑定到静态属性?我可以用C#来做什么

更新:是否可以执行单向源绑定?我知道双向是不可能的,因为静态对象上没有更新事件(至少在.NET4中是这样)。我无法实例化对象,因为它是静态的。

单向绑定

假设您有一个带有静态属性
Name
的类
Country

public class Country
{
  public static string Name { get; set; }
}
现在您需要将属性
Name
绑定到
TextBlock
TextProperty

Binding binding = new Binding();
binding.Source = Country.Name;
this.tbCountry.SetBinding(TextBlock.TextProperty, binding);
更新:双向绑定

Country
类如下所示:

public static class Country
    {
        private static string _name;

        public static string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                Console.WriteLine(value); /* test */
            }
        }
    }
现在我们希望将此属性
名称
绑定到
文本框
,因此:

Binding binding = new Binding();
binding.Source = typeof(Country);
binding.Path = new PropertyPath(typeof(Country).GetProperty("Name"));
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
this.tbCountry.SetBinding(TextBox.TextProperty, binding);
如果要更新目标,必须使用
BindingExpression
和函数
UpdateTarget

Country.Name = "Poland";

BindingExpression be = BindingOperations.GetBindingExpression(this.tbCountry, TextBox.TextProperty);
be.UpdateTarget();

您总是可以编写一个非静态类来提供对静态类的访问

静态类:

namespace SO.Weston.WpfStaticPropertyBinding
{
    public static class TheStaticClass
    {
        public static string TheStaticProperty { get; set; }
    }
}
非静态类提供对静态属性的访问

namespace SO.Weston.WpfStaticPropertyBinding
{
    public sealed class StaticAccessClass
    {
        public string TheStaticProperty
        {
            get { return TheStaticClass.TheStaticProperty; }
        }
    }
}
绑定很简单:

<Window x:Class="SO.Weston.WpfStaticPropertyBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SO.Weston.WpfStaticPropertyBinding"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:StaticAccessClass x:Key="StaticAccessClassRes"/>
    </Window.Resources>
    <Grid>
        <TextBlock Text="{Binding Path=TheStaticProperty, Source={StaticResource ResourceKey=StaticAccessClassRes}}" />
    </Grid>
</Window>

<Window x:Class="SO.Weston.WpfStaticPropertyBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SO.Weston.WpfStaticPropertyBinding"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:StaticAccessClass x:Key="StaticAccessClassRes"/>
    </Window.Resources>
    <Grid>
        <TextBlock Text="{Binding Path=TheStaticProperty, Source={StaticResource ResourceKey=StaticAccessClassRes}}" />
    </Grid>
</Window>