3
我正在使用MVVM编写WPF应用程序。我有我的项目的ObservableCollection:ObservableCollection <T>项目更新事件
public ObservableCollection<VarValue> Values;
public class VarValue: INotifyPropertyChanging, INotifyPropertyChanged
{
public double value
{
get
{
return this._value;
}
set
{
if (this._value != value)
{
this.OnvalueChanging(value);
this.SendPropertyChanging();
this._value = value;
this.SendPropertyChanged("value");
this.OnvalueChanged();
}
}
}
}
被绑定到DataGrid1中:
dataGrid1.ItemsSource = Values;
<DataGrid EnableColumnVirtualization="true" EnableRowVirtualization="true" Name="dataGrid1" DockPanel.Dock="Top" AutoGenerateColumns="False" Height="120" Width="Auto" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" SelectionUnit="Cell">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate><TextBlock Text='{Binding Values.value}'/></DataTemplate></DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate><TextBox Text='{Binding Values.value, Mode=TwoWay}'/></DataTemplate></DataGridTemplateColumn.CellEditingTemplate>
</DataGrid>
当用户编辑DataGrid1中的细胞,我需要创建新的VarValue对象,并将其插入到现有的集合。例如:
- 数据网格包含具有值一个单元= 1个
- 用户改变它来值= 2
- 现在在表必须有两个细胞:用1倍2的值。
我该如何做到这一点?
THX。这很有帮助 –