我想,一旦我改变了 txtA 的值,txtB 和 txtC 就会自动改变,因为我已经为 ValueA 实现了 INotifyPropertyChanged。
但是它们没有在 UI 上更新。txtB 始终为 100,而 txtC 始终为 -50。
我不知道是什么原因。
我的 Xaml..
<Window x:Class="WpfApplicationReviewDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBox Name="txtA" Text="{Binding ValueA}" />
<TextBox Name="txtB" Text="{Binding ValueB}" />
<TextBox Name="txtC" Text="{Binding ValueC}" />
</StackPanel>
</Window>
我后面的代码...
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new Model();
}
}
public class Model : INotifyPropertyChanged
{
private decimal valueA;
public decimal ValueA { get {
return valueA;
}
set
{
valueA = value;
PropertyChanged(this, new PropertyChangedEventArgs("ValueA"));
}
}
private decimal valueB;
public decimal ValueB
{
get
{
valueB = ValueA + 100;
return valueB;
}
set
{
valueB = value;
PropertyChanged(this, new PropertyChangedEventArgs("ValueB"));
}
}
private decimal valueC;
public decimal ValueC
{
get
{
valueC = ValueA - 50;
return valueC;
}
set
{
valueC = value;
PropertyChanged(this, new PropertyChangedEventArgs("ValueC"));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
在我将代码添加到 ValueA 属性的 set 方法后,它就可以工作了。
public decimal ValueA { get {
return valueA;
}
set
{
valueA = value;
PropertyChanged(this, new PropertyChangedEventArgs("ValueA"));
PropertyChanged(this, new PropertyChangedEventArgs("ValueB"));
PropertyChanged(this, new PropertyChangedEventArgs("ValueC"));
}
}
但我认为它应该为 txtB 和 txtC 自动刷新/更新。请指教。