我在 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"/>
窗口代码
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?如何解决?谢谢。