wpf 数据网格中的 Lostfocus 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12892210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lostfocus event in a datagrid
提问by Laya
I want lost focus event to be implemented for a column in a datagrid in wpf. I have used CellEditEnding event but this is getting raised on the start of editing not at the end. Please someone help me.
我希望为 wpf 中数据网格中的列实现失去焦点事件。我已经使用了 CellEditEnding 事件,但这是在编辑开始时而不是在结束时引发的。请有人帮助我。
回答by George
The solution provided by Esam works needs a little edit:
Esamworks 提供的解决方案需要稍微修改一下:
XAML remains the same:
XAML 保持不变:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Code behind to be adapted:
后面要改编的代码:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
var myCell = sender as DataGridCell;
if (myCell.Column.Header.ToString() == "Name of the column")
{
MessageBox.Show("tralala. I got it");
//do whatever needed here
}
}
回答by Dhaval Patel
i have used that event and it's working perfect. my xaml Code Look like
我已经使用了那个事件,它运行得很好。我的 xaml 代码看起来像
<DataGrid AutoGenerateColumns="False" Height="256" HorizontalAlignment="Left" Name="dgEmp" VerticalAlignment="Top" Width="490" ItemsSource="{Binding DeleteDate,Mode=TwoWay}" Margin="6,7,0,0" CanUserDeleteRows="True" RowEditEnding="dgEmp_RowEditEnding" CellEditEnding="dgEmp_CellEditEnding" Grid.RowSpan="3">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=TwoWay}" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Amount" Binding="{Binding Amount,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Date" Binding="{Binding Date,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Remark" Binding="{Binding Remark,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Update" x:Name="btnUpdate"
Click="btnUpdate_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="btnDelete"
Click="btnDelete_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
in my .cs file Code look like
在我的 .cs 文件中代码看起来像
private void dgEmp_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
var update = from p in Context.Expense_Submits where p.ID == objEmpToEdit.ID select p;
foreach (var item in update)
{
item.ID = objEmpToEdit.ID;
item.Description = objEmpToEdit.Description;
item.Date = objEmpToEdit.Date;
item.Amount = objEmpToEdit.Amount;
item.Remark = objEmpToEdit.Remark;
}
Context.SubmitChanges();
MessageBox.Show("The Current row updation is complete..");
}
private void dgEmp_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (isUpdateMode) //The Row is edited
{
Expense_Submit exp_sub = (from exp in submit where exp.ID == objEmpToEdit.ID select exp).FirstOrDefault();
FrameworkElement element_1 = dgEmp.Columns[1].GetCellContent(e.Row);
if (element_1.GetType() == typeof(TextBox))
{
var Description = ((TextBox)element_1).Text;
objEmpToEdit.Description = Description.ToString();
}
FrameworkElement element_2 = dgEmp.Columns[2].GetCellContent(e.Row);
if (element_2.GetType() == typeof(TextBox))
{
var Amount = ((TextBox)element_2).Text;
objEmpToEdit.Amount = Amount.ToString();
}
FrameworkElement element_3 = dgEmp.Columns[3].GetCellContent(e.Row);
if (element_3.GetType() == typeof(TextBox))
{
var Date = ((TextBox)element_3).Text;
objEmpToEdit.Date = Convert.ToDateTime(Date);
}
FrameworkElement element_4 = dgEmp.Columns[4].GetCellContent(e.Row);
if (element_4.GetType() == typeof(TextBox))
{
var Remark = ((TextBox)element_4).Text;
objEmpToEdit.Remark = Remark.ToString();
}
}
}
private void dgEmp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
objEmpToEdit = dgEmp.SelectedItem as Expense_Submit;
}
回答by Esam Sherif
Why not use LostFocus event on cells, then check if the cell belongs to the column you want.
为什么不在单元格上使用 LostFocus 事件,然后检查单元格是否属于您想要的列。
For example:
例如:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Then in the handler:
然后在处理程序中:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
if (((DataGridCell)sender).Column.Header == "My Column")
//do stuff
}

