我希望为 wpf 中的数据网格中的列实现失去焦点事件。我已经使用了 CellEditEnding 事件,但这是在编辑开始时而不是在结束时提出的。请有人帮助我。
问问题
9179 次
3 回答
1
Esam works 提供的解决方案需要稍作修改:
XAML 保持不变:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
后面要修改的代码:
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
}
}
于 2018-11-21T16:26:54.140 回答
0
为什么不对单元格使用 LostFocus 事件,然后检查该单元格是否属于您想要的列。
例如:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
然后在处理程序中:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
if (((DataGridCell)sender).Column.Header == "My Column")
//do stuff
}
于 2014-07-03T15:44:48.817 回答
0
我已经使用了该活动,并且运行良好。我的 xml 代码看起来像
<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>
在我的 .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;
}
于 2012-10-15T09:23:39.500 回答