是否可以绑定到 Properties.Settings.Default 中的值?
是否可以绑定到 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}"/>
我希望数据绑定在属性更改时随时更新。有办法做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对您来说很方便,
ApplicationSettingsBase
实现了INotifyPropertyChanged
,因此您只需在Properties.Settings.Default
上订阅PropertyChanged
即可提出您自己的PropertyChanged
作为响应。Conveniently for you,
ApplicationSettingsBase
implementsINotifyPropertyChanged
so you just need to subscribe toPropertyChanged
onProperties.Settings.Default
and raise your ownPropertyChanged
in response.您需要您的属性实现
INotifyPropertyChanged
,以便绑定能够识别属性值何时发生更改。You need your properties to implement
INotifyPropertyChanged
in order for bindings to recognise when the property value changes.