简体   繁体   English

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

[英]How to capture value change event of Datagrid's cell in WPF?

I'm using Datagrid to show some MySQL tables.Naturally,I expect that changes in datagrid should be reflected in MySQL table.I've write a command to handle the Cell value changed event ,however,the command did not hit: 我正在使用Datagrid显示一些MySQL表。自然地,我希望datagrid的更改应反映在MySQL表中。我编写了一个命令来处理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>

I think that I was listening to the wrong event,because my updateCell did not hit...How do you catch cell value changed event of datagrid? 我认为我正在听错事件,因为我的updateCell没有命中...您如何捕获datagrid的cell value changed event

I've figured out a solution. 我想出了一个解决方案。

First add the related ViewModel as the DataGrid's DataContext .otherwise the compiler won't know where to find the binding command. 首先添加相关的ViewModel作为DataGrid's DataContext否则编译器将不知道在哪里可以找到绑定命令。

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

Then change TargetUpdated to CurrentCellChanged . 然后将TargetUpdated更改为CurrentCellChanged

Why would you want to attach an event? 您为什么要附加活动? Usually you will create a DataBinding for your Column against some Property of an object in an ObservableCollection. 通常,您将根据ObservableCollection中对象的某些属性为列创建DataBinding。 Changes in a Grid's cell will be directly pushed into the setter of the Property you bound to. 网格单元格中的更改将直接推送到绑定到的属性的设置器中。 From there you can do whatever pleases you, like writing to the db or set a modified flag. 从那里,您可以做任何让您满意的事情,例如写入db或设置修改的标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM