你可以试试如下的解决方案,使用LostKeyboardFocus事件调用EndEdit方法。
思路就是判断当获得焦点的元素不是DataGrid,也不是DataGrid的子元素上的时候,调用EndEdit方法
- void grid_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
- {
- var focused = Keyboard.FocusedElement;
- if (focused != null)
- {
- var parent = C1.WPF.VTreeHelper.GetParentOfType((DependencyObject)focused, typeof(C1DataGrid));
- if (parent == null)
- {
- this.grid.EndEdit();
- }
- }
- }
复制代码 |