WPF DataGrid 计算
WPF DataGrid calculations
我是 WPF 和 C# 的新手,所以请帮助解决这个问题。
我有一个DataGrid
,列如下:"One", "Two", "Multiply".
我输入数字 "One" 或 "Two" 并在列 "Multiply" 中得到结果的想法。
当我编写代码并对其进行调试时,我可以看到 属性 的值正在重新计算。但是,除非我按下 space 栏或单击我的最后一列,否则我不会在最后一列中显示。
代码如下:
public class Numbers : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double _one;
private double _two;
private double _multiply;
public double One
{
get { return _one; }
set { _one = value; UpdateValue(); }
}
public double Two
{
get { return _two; }
set { _two = value; UpdateValue(); }
}
public double Multiply
{
get { return _multiply; }
set { _multiply = value; UpdateValue(); }
}
public Numbers(double pOne, double pTwo)
{
_one = pOne;
_two = pTwo;
_multiply = GetMultiply();
}
private void UpdateValue()
{
OnPropertyChanged("One");
OnPropertyChanged("Two");
OnPropertyChanged("Multiply");
_multiply = GetMultiply();
}
private double GetMultiply()
{
return _one * _two;
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
public class Collection : ObservableCollection<Numbers>
{
public ObservableCollection<Numbers> Numbers { get; set; }
public Collection()
{
Numbers = new ObservableCollection<Numbers>();
Numbers.Add(new Numbers(1, 2));
Numbers.Add(new Numbers(2, 2));
}
}
XAML:
<DataGrid x:Name="StockData" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" ItemsSource="{Binding}" AutoGenerateColumns="False" LostFocus="StockData_LostFocus" >
<DataGrid.Columns >
<DataGridTextColumn Header="Number One" Width="100" Binding="{Binding One, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:C\}}" />
<DataGridTextColumn Header="Number Two" Width="100" Binding="{Binding Two, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:C\}}" />
<DataGridTextColumn Header="Total" Width="100" Binding="{Binding Multiply, BindsDirectlyToSource=True, Mode=TwoWay, StringFormat=\{0:C\}, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
您在引发 PropertyChanged 事件后设置乘法,因此 UI 不会收到值已更改的通知。
设置乘法然后引发 PropertyChanged 事件:
private void UpdateValue()
{
_multiply = GetMultiply();
OnPropertyChanged("One");
OnPropertyChanged("Two");
OnPropertyChanged("Multiply");
}
我是 WPF 和 C# 的新手,所以请帮助解决这个问题。
我有一个DataGrid
,列如下:"One", "Two", "Multiply".
我输入数字 "One" 或 "Two" 并在列 "Multiply" 中得到结果的想法。
当我编写代码并对其进行调试时,我可以看到 属性 的值正在重新计算。但是,除非我按下 space 栏或单击我的最后一列,否则我不会在最后一列中显示。
代码如下:
public class Numbers : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double _one;
private double _two;
private double _multiply;
public double One
{
get { return _one; }
set { _one = value; UpdateValue(); }
}
public double Two
{
get { return _two; }
set { _two = value; UpdateValue(); }
}
public double Multiply
{
get { return _multiply; }
set { _multiply = value; UpdateValue(); }
}
public Numbers(double pOne, double pTwo)
{
_one = pOne;
_two = pTwo;
_multiply = GetMultiply();
}
private void UpdateValue()
{
OnPropertyChanged("One");
OnPropertyChanged("Two");
OnPropertyChanged("Multiply");
_multiply = GetMultiply();
}
private double GetMultiply()
{
return _one * _two;
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
public class Collection : ObservableCollection<Numbers>
{
public ObservableCollection<Numbers> Numbers { get; set; }
public Collection()
{
Numbers = new ObservableCollection<Numbers>();
Numbers.Add(new Numbers(1, 2));
Numbers.Add(new Numbers(2, 2));
}
}
XAML:
<DataGrid x:Name="StockData" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" ItemsSource="{Binding}" AutoGenerateColumns="False" LostFocus="StockData_LostFocus" >
<DataGrid.Columns >
<DataGridTextColumn Header="Number One" Width="100" Binding="{Binding One, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:C\}}" />
<DataGridTextColumn Header="Number Two" Width="100" Binding="{Binding Two, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:C\}}" />
<DataGridTextColumn Header="Total" Width="100" Binding="{Binding Multiply, BindsDirectlyToSource=True, Mode=TwoWay, StringFormat=\{0:C\}, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
您在引发 PropertyChanged 事件后设置乘法,因此 UI 不会收到值已更改的通知。
设置乘法然后引发 PropertyChanged 事件:
private void UpdateValue()
{
_multiply = GetMultiply();
OnPropertyChanged("One");
OnPropertyChanged("Two");
OnPropertyChanged("Multiply");
}