Is it possible to bind to a value in Properties.Se

2019-03-02 06:26发布

问题:

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.