【发布时间】:2014-07-09 20:39:03
【问题描述】:
我在 WPF 中有一个 DataGrid,如下所示:
上面的 DataGrid 行,代表以下示例模型:
class MyModel
{
public string Value { get; set; }
public string Message { get; set; }
}
我想防止在编辑文本框后按回车键时自动选择发生变化,因此在编辑第一行文本框内容时不会移动到第二行,右侧的标签将在某些条件后更新,例如:如果文本仅包含数字字符,则文本正常,否则出错。
我该怎么做?
我尝试通过使用我的视图 KeyPressed、PreviewKeyDown 和 LostFocus 事件背后的代码来做到这一点,但它不起作用:(
if (e.Key == Key.Enter || e.Key == Key.Return)
{
var dataGrid = (DataGrid) sender;
if (dataGrid != null)
{
var row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem) as DataGridRow;
base.OnPreviewKeyDown(e);
//e.Handled = true;
if (row != null)
dataGrid.SelectedItem = row.Item;
}
else
base.OnPreviewKeyDown(e);
}
【问题讨论】:
标签: c# wpf datagrid wpf-controls