简体   繁体   English

有时在绑定后引发DataGrid CellEditEnding事件

[英]DataGrid CellEditEnding event raised after binding sometimes

I have a WPF DataGrid bound to a collection of items. 我有一个WPF DataGrid绑定到项目的集合。 In the code behind I have subscribed to the CellEditEnding event: 在后面的代码中,我订阅了CellEditEnding事件:

dg.CellEditEnding += dg_CellEditEnding;

I hoped this event would be raised before the changed value was updated in the bound object. 我希望在绑定对象中更新更改的值之前会引发此事件。 But it appears that sometimes it does, and sometimes it doesn't. 但似乎有时确实如此,有时却没有。 If I change a value in the grid and press enter, the event is raised first, followed by the bound object being updated. 如果更改网格中的值并按Enter,则首先引发事件,然后更新绑定的对象。 If I press tab, or click on another cell in the same row, they happen in reverse. 如果我按Tab键,或单击同一行中的另一个单元格,它们将反向发生。

Why does this happen? 为什么会这样? I need a way to see a changed value before it is applied to the bound object. 我需要一种方法来查看更改的值,然后将其应用于绑定对象。 Is there another event I can subscribe to instead? 我还有其他活动可以订阅吗? I realize this is not the traditional MVVM approach. 我意识到这不是传统的MVVM方法。 This is part of a custom datagrid control with some requirements that are hard to implement in the viewmodel. 这是自定义数据网格控件的一部分,其中包含一些很难在视图模型中实现的要求。

you could use the same body for the CellEditEnding event for a "LostFocus" event. 您可以对“ LostFocus”事件的CellEditEnding事件使用相同的主体。 This way, whatever you want to happen when its done editing also happens when you leave the cell. 这样,无论您希望在完成编辑后发生什么,都会在离开单元格时发生。

At least, that's how I expect it to work. 至少,这就是我期望它起作用的方式。 Try it out and let me know! 试试看,让我知道!

Here is a link to all the events for datagridcells, if you dont think LostFocus will work for what you're looking for. 如果您认为LostFocus不能满足您的需求,那么这里是指向 datagridcells所有事件的链接

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

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