【问题标题】:Is it possible to bind to a value in Properties.Settings.Default?是否可以绑定到 Properties.Settings.Default 中的值?
【发布时间】:2011-05-02 19:00:52
【问题描述】:

是否可以绑定到 Properties.Settings.Default 中的值,以使我的 UI 与存储在那里的值保持同步?

我有一堂课:

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

在我的 XAML 中,我有一个资源:

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

还有一个绑定:

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

我希望数据绑定在属性更改时随时更新。有没有办法做到这一点?

【问题讨论】:

    标签: wpf data-binding properties.settings


    【解决方案1】:

    为您方便,ApplicationSettingsBase 实现了INotifyPropertyChanged,因此您只需要在Properties.Settings.Default 上订阅PropertyChanged 并提出您自己的PropertyChanged 作为回应。

    【讨论】:

    • 施威特! (这些额外的字符出租。)
    【解决方案2】:

    您需要您的属性实现INotifyPropertyChanged,以便绑定能够识别属性值何时更改。

    【讨论】:

    • 为了让我的属性发出通知,他们需要知道他们正在读取的默认属性何时更改。这给我带来了完整的循环。
    • @Jekke:请参阅 Rick 对此的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2015-06-18
    • 2021-10-06
    相关资源
    最近更新 更多