简体   繁体   English

绑定到静态类实例中的属性

[英]Binding to a property within a static class instance

What I am trying to achieve 我正在努力实现的目标

I have a WPF application (it's just for testing) and I want to bind the text (Content) of a label to a property somewhere. 我有一个WPF应用程序(仅用于测试),我想将标签的文本(内容)绑定到某个位置的属性。 The idea is that this property value will be changed when the user chooses a different language. 想法是,当用户选择其他语言时,将更改此属性值。 When the property changes, I want the label text to update with the new value. 当属性更改时,我希望标签文本以新值更新。

What I have tried 我尝试过的

I tried to create a static class with a static property for the label value. 我试图为标签值创建一个带有静态属性的静态类。 For example: 例如:

public static class Language
{
    public static string Name = "Name";
}

I then was able to bind this value to my label using XAML like so: 然后,我可以使用XAML将此值绑定到标签上,如下所示:

Content="{Binding Source={x:Static lang:Language.Name}}"

And this worked fine for showing the initial value of "Name". 这样做可以很好地显示“名称”的初始值。 The problem is, when the Name property changes the label value doesn't change. 问题是,当Name属性更改时,标签值不会更改。

So, back to the drawing board (Google). 因此,回到绘图板(Google)。 Then I found this answer which sounded exactly like what I needed. 然后我找到了这个答案 ,听起来完全符合我的需求。 So here was my new attempt at this: 因此,这是我的新尝试:

public class Language
{
    public static Language Instance { get; private set; }
    static Language() { Instance = new Language(); }
    private Language() { }

    private string name = "Name";
    public string Name { get { return name; } set { name = value; } }
}

With my binding changed it this: 用我的绑定更改了它:

Content="{Binding Source={x:Static lang:Language.Instance}, Path=Name}"

This still results in the same problem. 这仍然会导致相同的问题。

Questions 问题

What am I missing here? 我在这里想念什么? How can I get the label to update when the value is changed? 值更改后如何更新标签?

That simply isn't a property. 那根本不是财产。 Try: 尝试:

public class Language
{
    public static Language Instance { get; private set; }
    static Language() { Instance = new Language(); }
    private Language() { Name = "Name"; }

    public string Name {get;private set;}
}

or with change notification: 或带有更改通知:

public class Language : INotifyPropertyChanged
{
    public static Language Instance { get; private set; }
    static Language() { Instance = new Language(); }
    private Language() { }

    private string name = "Name";
    public string Name
    {
        get { return name; }
        set { SetValue(ref name, value);}
    }
    protected void SetValue<T>(ref T field, T value,
        [CallerMemberName]string propertyName=null)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(propertyName);
        }
    }
    protected virtual void OnPropertyChanged(
        [CallerMemberName]string propertyName=null)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM