DataGrid CellEditEnding事件未在DataGridTemplateColumn内的单元格编辑上触发 [英] DataGrid CellEditEnding event not firing on a cell editing inside DataGridTemplateColumn
问题描述
在MyDatagrid_CellEditEnding(...)
事件(如下所示)中,每当用户编辑三列之一的单元格时,我都会捕获单元格的编辑值.编辑第二列或第三列中的单元格后,当我移出该单元格时,可以看到CellEditEnding
事件被调用.但是对于DataGridTemplateColumn
列的第一列却不是这样.也就是说,当我在第一个DataGridTemplateColumn
中的任何单元格中更改日期并将光标移出该单元格时,不会调用CellEditEnding
事件.
In the MyDatagrid_CellEditEnding(...)
event (shown below) I am capturing the edited value of a cell whenever a cell of one of the three columns is edited by a user. After editing cell in second or third column, when I move out of that cell, I can see the CellEditEnding
event getting called. But the same is not true for the first column that is a DataGridTemplateColumn
column. That is, when I change a date in any cell in the first DataGridTemplateColumn
, and move the cursor out of the cell, the CellEditEnding
event is not called.
问题:我在这里可能会缺少什么,我们如何使它起作用?我在网上看到了类似的问题及其解决方案(例如 this 和
Question: What I may be missing here and how can we make it work? I have seen similar issues and their solutions online (such as this and this), so I'm not sure what I may be missing here.
注意:我正在Windows 10 vs1903 - Pro
<Window x:Class="MyTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
.....
Title="MainWindow">
<Grid>
<DataGrid x:Name="MyDatagrid" AutoGenerateColumns="False" SelectionMode="Single" CellEditEnding="MyDatagrid_CellEditEnding">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Date Modified">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding DateModified}" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding DateModified}" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="btnTest" Content="Test" HorizontalAlignment="Left" VerticalAlignment="Top" Click="btnTest_Click"/>
</Grid>
</Window>
代码:
......
......
string _sDateModified;
string _sFirstName;
string _sLastName;
.....
.....
private void MyDatagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridColumn c = e.Column;
if (c.Header.ToString() == "Date Modified")
_sDateModified = (e.EditingElement as TextBox).Text;
else if (c.Header.ToString() == "First Name")
_sFirstName = (e.EditingElement as TextBox).Text;
else if (c.Header.ToString() == "Last Name")
_sLastName = (e.EditingElement as TextBox).Text;
}
推荐答案
您正在CellTemplate
中编辑DatePicker
,但这不会导致CellEditEnding
事件触发. CellTemplate
不应包含任何输入控件.
You are editing the DatePicker
in the CellTemplate
and this doesn't cause the CellEditEnding
event to fire. The CellTemplate
is not supposed to contain any input controls.
用TextBlock
替换CellTemplate
中的DatePicker
:
<DataGridTemplateColumn Header="Date Modified">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DateModified, StringFormat=yyyy-MM-dd}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding DateModified, UpdateSourceTrigger=LostFocus}" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
或将CellTemplate
中DatePicker
的IsEnabled
属性设置为false
.无论哪种方式,您都必须通过双击单元格进入编辑模式,然后才能触发CellEditEnding
事件.
Or set the IsEnabled
property of the DatePicker
in the CellTemplate
to false
. Either way, you'll have to enter the edit mode by double clicking on the cell before you can expect the CellEditEnding
event to fire.
这篇关于DataGrid CellEditEnding事件未在DataGridTemplateColumn内的单元格编辑上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!