如何刷新从Singleton加载的wpf文本绑定?

时间:2022-09-02 08:38:47

I have a singleton class that has LanguageModel in it and i have already done text binding using following code

我有一个单独的类,其中包含LanguageModel,我已经使用以下代码完成了文本绑定

{Binding Source={StaticResource Language}, Path=Language.HeaderText}

how i want to update this value at runtime. Can anyone suggest me how do I update this in runtime? Where "Language" is inside a singleton class.

我想如何在运行时更新此值。任何人都可以建议我如何在运行时更新它? “语言”在单身人士类中。

3 个解决方案

#1


1  

WPF UI updates are built on INotifyPropertyChanged implementation. For whichever properties you want to see an update you would need to trigger NotifyPropertyChanged event.

WPF UI更新基于INotifyPropertyChanged实现构建。对于要查看更新的任何属性,您需要触发NotifyPropertyChanged事件。

If you don't want to touch your Language model with 200 members. Identify the properties for which you want to see the updates and move them to a ViewModel and implement the NotifyPropertyChanged within ViewModel. But mind that, this will only reflect if you are updating the Value from ViewModel. If the Langauge model gets directly updated it won't notify the ViewModel which inturn wont notify the UI.

如果您不想触及200名成员的语言模型。确定要查看更新的属性,并将它们移动到ViewModel并在ViewModel中实现NotifyPropertyChanged。但请注意,这只会反映您是否正在从ViewModel更新Value。如果Langauge模型直接更新,它将不会通知ViewModel哪个内部不会通知UI。

#2


2  

You need to implement the INotifyPropertyChanged Interface.

您需要实现INotifyPropertyChanged接口。

If your class implements this interface the bindings will update automatically when you change the property value.

如果您的类实现此接口,则更改属性值时绑定将自动更新。

Follow the documentation to implement the interface, but basically you'll need to notify the subscribers of your property that it has changed. You can do this by calling NotifyPropertyChanged() in the setter of your HeaderText property.

按照文档来实现界面,但基本上你需要通知你的财产的订户它已经改变。您可以通过在HeaderText属性的setter中调用NotifyPropertyChanged()来完成此操作。

Simplified code, you need to implement the actual interface as well:

简化代码,您还需要实现实际的接口:

public class Language : INotifyPropertyChanged {
    private string _headerText;

    public string HeaderText {
        get {
            return _headerText;
        }

        set {
            if (value != _headerText) {
                _headerText = value;
                NotifyPropertyChanged();
            }
        }
    }
}

#3


0  

I resolved it. I was missing "Mode=TwoWay" in the binding.

我解决了我在绑定中缺少“Mode = TwoWay”。

{Binding Source={StaticResource Language}, Path=Language.HeaderText, Mode=TwoWay}

Thanks for your help guys. Really appreciated.

谢谢你的帮助。非常感谢。

#1


1  

WPF UI updates are built on INotifyPropertyChanged implementation. For whichever properties you want to see an update you would need to trigger NotifyPropertyChanged event.

WPF UI更新基于INotifyPropertyChanged实现构建。对于要查看更新的任何属性,您需要触发NotifyPropertyChanged事件。

If you don't want to touch your Language model with 200 members. Identify the properties for which you want to see the updates and move them to a ViewModel and implement the NotifyPropertyChanged within ViewModel. But mind that, this will only reflect if you are updating the Value from ViewModel. If the Langauge model gets directly updated it won't notify the ViewModel which inturn wont notify the UI.

如果您不想触及200名成员的语言模型。确定要查看更新的属性,并将它们移动到ViewModel并在ViewModel中实现NotifyPropertyChanged。但请注意,这只会反映您是否正在从ViewModel更新Value。如果Langauge模型直接更新,它将不会通知ViewModel哪个内部不会通知UI。

#2


2  

You need to implement the INotifyPropertyChanged Interface.

您需要实现INotifyPropertyChanged接口。

If your class implements this interface the bindings will update automatically when you change the property value.

如果您的类实现此接口,则更改属性值时绑定将自动更新。

Follow the documentation to implement the interface, but basically you'll need to notify the subscribers of your property that it has changed. You can do this by calling NotifyPropertyChanged() in the setter of your HeaderText property.

按照文档来实现界面,但基本上你需要通知你的财产的订户它已经改变。您可以通过在HeaderText属性的setter中调用NotifyPropertyChanged()来完成此操作。

Simplified code, you need to implement the actual interface as well:

简化代码,您还需要实现实际的接口:

public class Language : INotifyPropertyChanged {
    private string _headerText;

    public string HeaderText {
        get {
            return _headerText;
        }

        set {
            if (value != _headerText) {
                _headerText = value;
                NotifyPropertyChanged();
            }
        }
    }
}

#3


0  

I resolved it. I was missing "Mode=TwoWay" in the binding.

我解决了我在绑定中缺少“Mode = TwoWay”。

{Binding Source={StaticResource Language}, Path=Language.HeaderText, Mode=TwoWay}

Thanks for your help guys. Really appreciated.

谢谢你的帮助。非常感谢。