【发布时间】:2015-09-20 08:11:30
【问题描述】:
对于 wpf 数据网格,我有以下两个事件:
1-CellEditEnding
2-PreviewKeyDown
当我在数据网格中的任何单元格中完成编辑时,首先运行 PreviewKeyDown 事件,然后运行 CellEditEnding。但我需要先运行 CellEditEnding,然后再运行 PreviewKeyDown。所以我通过编写以下代码在 CellEditEnding 事件中以编程方式调用 PreviewKeydown 事件。
private void maingrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
TextBox t = e.EditingElement as TextBox;
DataGridColumn dgc = e.Column;
if ((string)dgc.Header == "Product Id")
{
if (vm_order.PopulateProductRow(maingrid.SelectedIndex, Convert.ToInt32(t.Text)) == false)
{
MessageBox.Show("Product does not exists ", "Message", MessageBoxButton.OK);
}
}
**maingrid.PreviewKeyDown += new maingrid_PreviewKeyDown(maingrid_PreviewKeyDown);**
}
请查看我以编程方式调用 PrevewKeyDownEvent 的最后一行代码,它给出了错误。
我已经看到了 wpf datagrid 的 mouse_up 事件的示例,它工作得非常好。以下是代码供参考:
public EventsSample()
{
InitializeComponent();
pnlMainGrid.MouseUp += new MouseButtonEventHandler(pnlMainGrid_MouseUp);
}
private void pnlMainGrid_MouseUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("You clicked me at " + e.GetPosition(this).ToString());
}
那么为什么我不能将它用于 PreviewKeyDown 事件?
【问题讨论】:
-
您是否查看了 KeyDownEvent 而不是 PreviewKeyDown?预览前缀意味着此类事件必须领先
-
在代码行你不仅调用
PrevewKeyDownEvent处理程序,还尝试从中创建对象,并再次添加为事件。所以可能你只需要maingrid_PreviewKeyDown(sender, new KeyEventArgs()) -
我已经编辑了我的问题。请查看工作正常的第二个示例。我想对 wpd 数据网格的 PreviewKeyDown 或 KeyDown 事件完全相同,谢谢,