C# DataGrid Lostfocus事件触发DataGrid WPF上的每个操作

C# DataGrid Lostfocus事件触发DataGrid WPF上的每个操作,c#,wpf,datagrid,C#,Wpf,Datagrid,我只想在失控时更新DataGrid的值。为此,我使用了datagrid的LostFocus事件。但是这个事件触发了datagrid的每一个动作。例如,当我点击单元格进行编辑时,它将被触发 Control ctrl=FocusManager.GetFocusedElement(this)作为控件;始终为空:( 如果我没记错的话,DataGrid会失去焦点,因为内部控件确实收到了它 请使用属性或与此事件一起尝试。当DataGridCell失去焦点时,它会触发DataGrid失去焦点事件。您需要处理单

我只想在失控时更新DataGrid的值。为此,我使用了datagrid的LostFocus事件。但是这个事件触发了datagrid的每一个动作。例如,当我点击单元格进行编辑时,它将被触发

Control ctrl=FocusManager.GetFocusedElement(this)作为控件;始终为空:(


如果我没记错的话,DataGrid会失去焦点,因为内部控件确实收到了它


请使用属性或与此事件一起尝试。

DataGridCell
失去焦点时,它会触发
DataGrid
失去焦点事件。您需要处理单元格事件

将此样式放入您的
DataGrid

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <EventSetter Event="LostFocus" Handler="DataGridCell_LostFocus"/>
    </Style>
</DataGrid.CellStyle>
编辑:由于您是动态创建DataGrid的,下面是如何设置DataGrid单元格的样式以订阅上面的失去焦点事件

制作一个玛瑙

 private delegate void Lost_Focus_Delegate(object sender, RoutedEventArgs e);
然后在数据网格生成中

 Style style = new Style
 {
     TargetType = typeof(DataGridCell)
 };

 style.Setters.Add(new EventSetter(LostFocusEvent, new RoutedEventHandler(new Lost_Focus_Delegate(this.DataGridCell_LostFocus))));

 dataGrid.CellStyle = style;

我刚刚意识到您的DataGrid是动态创建的。理论上,您只需要动态地设置DataGrid的DataGridCell的样式。
 private delegate void Lost_Focus_Delegate(object sender, RoutedEventArgs e);
 Style style = new Style
 {
     TargetType = typeof(DataGridCell)
 };

 style.Setters.Add(new EventSetter(LostFocusEvent, new RoutedEventHandler(new Lost_Focus_Delegate(this.DataGridCell_LostFocus))));

 dataGrid.CellStyle = style;