如何在WPF中捕获Datagrid单元格的值更改事件?

时间:2014-05-27 07:07:34

标签: c# mysql wpf datagrid

我使用Datagrid来显示一些MySQL表。自然,我希望数据网格中的更改应该反映在MySQL表中。我已经编写了一个命令来处理{{1}但是,命令没有命中:

Cell value changed event

我认为我正在听错了事件,因为我的<DataGrid x:Name="Table_DataGrid" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0" ItemsSource="{Binding MyServerWrapViewModel.TableFromMySql, Source={StaticResource Locator}}"> <i:Interaction.Triggers> <i:EventTrigger EventName="TargetUpdated"> <cmd:EventToCommand Command="{Binding updateCell}"/> </i:EventTrigger> </i:Interaction.Triggers> </DataGrid> 没有击中......你怎么抓住updateCell数据网格?

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。

首先将相关的ViewModel添加为DataGrid's DataContext。否则编译器将不知道在哪里找到绑定命令。

DataContext="{Binding MyServerWrapViewModel,Source={StaticResource Locator}}"

然后将TargetUpdated更改为CurrentCellChanged

答案 1 :(得分:0)

为什么要附加活动?通常,您将针对ObservableCollection中对象的某些属性为您的列创建一个DataBinding。 Grid的单元格中的更改将直接推送到您绑定的Property的setter中。从那里你可以做任何你喜欢的事情,比如写入数据库或设置修改后的标志。

相关问题