我在UI中有一个只读文本框,它绑定到Properties.Settings.Default.MyVar,当窗口打开时,绑定正确地获得值。但是当用户单击一个按钮(这个按钮改变Properties.Setting.Default.MyVar)时,文本框不会更新(但如果我关闭窗口并再次打开它,我就会得到新值)。我已经试过UpdataSourceTrigger了,但不起作用。
我的xml:
<TextBox IsReadOnly="True"
Text="{Binding Source={StaticResource settings}, Path=MyVar}"/>
<Button Content="..." Click="ChangeMyVar_Click"/>window的代码
public partial class ConfigureWindow : Window, INotifyPropertyChanged
{
public ConfigureWindow()
{
InitializeComponent();
}
private void ChangeMyVar_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.MyVar = "Changed";
Properties.Settings.Default.Save();
OnPropertyChanged("MyVar");
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(info));
}
}调试时,我发现处理程序总是为空。我的INotifyPropertyChanged实现错误了吗?或者我不能使用Properties.Settings更新UI?如何解决这个问题?谢谢。
发布于 2012-08-13 01:34:34
这一点:
Source={StaticResource settings}看起来您没有绑定到默认设置,而是绑定到另一个实例,所以如果您更改默认设置,绑定当然不会更新,因为它的源代码根本没有更改。使用:
xmlns:prop="clr-namespace:WpfApplication.Properties"Source={x:Static prop:Settings.Default}更改属性就足够了,对于UI要注意到的更改,包含属性的类需要触发更改通知,因此您的通知不会执行任何操作。但是,在这种情况下,您根本不需要做任何事情,因为应用程序设置类实现了INPC,您只需要绑定到正确的实例。
https://stackoverflow.com/questions/11924109
复制相似问题