Is it possible to bind to a value in Properties.Settings.Default?

喜欢而已 提交于 2019-12-20 05:13:18

问题


Is it possible to bind to a value in Properties.Settings.Default in a way that will keep my UI current with the value stored there?

I have a class:

public class FavoritePlayer
{
    public string Name
    {
        get
        {
            return wpfSample021.Properties.Settings.Default.FavoritePlayer.ToString();
        }
        set
        {
            wpfSample021.Properties.Settings.Default.FavoritePlayer = value;
        }
    }
}

In my XAML, I have a resource:

    <local:FavoritePlayer x:Key="fvPlayer"/>

and a binding:

    <Label DataContext="{DynamicResource fvPlayer}" Content="{Binding Path=Name}"/>

What I would like is for the data-binding to update any time the property is changed. Is there a way to do this?


回答1:


Conveniently for you, ApplicationSettingsBase implements INotifyPropertyChanged so you just need to subscribe to PropertyChanged on Properties.Settings.Default and raise your own PropertyChanged in response.




回答2:


You need your properties to implement INotifyPropertyChanged in order for bindings to recognise when the property value changes.



来源:https://stackoverflow.com/questions/5861238/is-it-possible-to-bind-to-a-value-in-properties-settings-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!