我有两次数据网格插入/更新行的问题。 datagrid绑定到一个可加上ItemEndEdit事件的可观察集合:
/// <summary>
/// extends the ObservableCollection class to include adding an ItemEndEdit event
/// </summary>
public class ObservableProjectExpenseItems : ObservableCollection<ProjectExpenseItemsBO>
{
protected override void InsertItem(int index, ProjectExpenseItemsBO item)
{
base.InsertItem(index, item);
item.ItemEndEdit += new ProjectExpenseItemsBO.ItemEndEditEventHandler((x) =>
{
if (ItemEndEdit != null)
ItemEndEdit(x);
});
}
public event ProjectExpenseItemsBO.ItemEndEditEventHandler ItemEndEdit;
}
ProjectExpenseItemsBO是我的模型/业务对象,它包含绑定在dg列中的属性。
我在viewmodel中使用以下内容在用户离开datagrid的行时插入/更新记录:
void ProjectExpenseItemsItemEndEdit(IEditableObject sender)
{
ProjectExpenseItemsBO projectExpenseItemsBO = sender as ProjectExpenseItemsBO;
if (projectExpenseItemsBO.RowID == 0)
{
// if the ProjectExpenseItemsBO is a new row
projectExpenseItemsRepository.AddProjectExpenseItem(projectExpenseItemsBO, this.ProjectExpenseID);
}
else
{
projectExpenseItemsRepository.UpdateProjectExpenseItem(projectExpenseItemsBO);
}
// get the total of the project expense items
ItemTotal = projectExpenseItemsRepository.GetItemTotal(this.ProjectExpenseID);
}
更新和插入两次火,我无法弄清楚原因。我在类/ viewmodel的初始化中附加了ItemEndEdit:
ObservableProjectExpenseItems projectExpenseItemsCollection;
List<ProjectExpenseItemsBO> list = new List<ProjectExpenseItemsBO>(projectExpenseItemsRepository.GetProjectExpenseItems(this.ProjectExpenseID));
list.ForEach(item => ProjectExpenseItemsCollection.Add(item));
ProjectExpenseItemsCollection.ItemEndEdit += new ProjectExpenseItemsBO.ItemEndEditEventHandler(ProjectExpenseItemsItemEndEdit);
最后,这是我的datagrid:
<DataGrid ItemsSource="{Binding Path=ProjectExpenseItemsCollection}" Grid.Row="0" Grid.Column="0" AutoGenerateColumns="False"
Name="dgProjectExpenseItems" SelectionMode="Single" SelectionUnit="FullRow" CanUserResizeColumns="True"
SelectedItem="{Binding Path=SelectedProjectExpenseItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
GridLinesVisibility="Horizontal" CanUserDeleteRows="True" CanUserAddRows="True">
<DataGrid.RowValidationRules>
<vr:RowDataInfoValidationRule ValidationStep="UpdatedValue" />
</DataGrid.RowValidationRules>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="SizeToCells" IsReadOnly="True" MinWidth="50" Binding="{Binding RowID}" />
<DataGridTextColumn Header="Project Expense ID" IsReadOnly="True" Width="SizeToCells" Visibility="Hidden" MinWidth="0" Binding="{Binding ProjectExpenseID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Item Number" Width="SizeToCells" MinWidth="140" Binding="{Binding ItemNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Item Description" Width="SizeToCells" MinWidth="250" Binding="{Binding ItemDescription, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<!--<DataGridTextColumn Header="Qty" Width="SizeToCells" MinWidth="65" Binding="{Binding ItemQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
<DataGridTextColumn Header="Qty">
<DataGridTextColumn.Binding>
<Binding Path="ItemQty" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<vr:PositiveDecimalValidationRule ValidationStep="RawProposedValue" />
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="Unit Price">
<DataGridTextColumn.Binding>
<Binding Path="ItemUnitPrice" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<vr:PositiveDecimalValidationRule ValidationStep="RawProposedValue" />
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<!--<DataGridTextColumn Header="Unit Price" Width="SizeToCells" MinWidth="90" Binding="{Binding ItemUnitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
<DataGridTextColumn Header="Supplier Name" Width="SizeToCells" MinWidth="200" Binding="{Binding SupplierName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
好的。在试图找出我遇到的另一个问题之后,我偶然发现导致事件发生两次的原因。当我在datagrid中保存一行时,我注意到保存前后主键字段值为零。显然,这是因为我在调用viewmodel中的save方法后没有刷新集合。当我在viewmodel中更改代码以刷新集合时:
void ProjectExpenseItemsItemEndEdit(IEditableObject sender)
{
ProjectExpenseItemsBO projectExpenseItemsBO = sender as ProjectExpenseItemsBO;
if (projectExpenseItemsBO.RowID == 0)
{
// use the data access layer to update the wrapped data object
projectExpenseItemsRepository.AddProjectExpenseItem(projectExpenseItemsBO, this.ProjectExpenseID);
}
else
{
projectExpenseItemsRepository.UpdateProjectExpenseItem(projectExpenseItemsBO);
}
//refresh the collection
ProjectExpenseItemsCollection.Clear();
List<ProjectExpenseItemsBO> list = new List<ProjectExpenseItemsBO>(projectExpenseItemsRepository.GetProjectExpenseItems(this.ProjectExpenseID));
list.ForEach(item => ProjectExpenseItemsCollection.Add(item));
// get the total of the project expense items
ItemTotal = projectExpenseItemsRepository.GetItemTotal(this.ProjectExpenseID);
}
重复记录的问题消失了。哇,我一直在研究这个问题几个星期了。有趣的是,如此基本的东西可以让你长时间循环,特别是当你想要学习新的东西时(wpf,mvvm)。