WPF 数据网格“shift + tab”(反向制表符)在启用“单击编辑”后不再工作?
WPF datagrid “shift + tab” (reverse tabbing) no longer working after enabled “single-click edit”?
我有一个可编辑的数据网格,我想在数据网格单元格上实现 "single-click edit",这意味着您只需单击一次即可使数据网格单元格可编辑。
<DataGrid RowHeaderWidth="0" BorderThickness="1,1,1,0" GridLinesVisibility="All"
AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserDeleteRows="False"
EnableColumnVirtualization="False" EnableRowVirtualization="False" SelectionMode="Single"
ItemsSource="{Binding Path=Queries}"
SelectedIndex="{Binding Path=SelectedQueryIndex}"
DataGridCell.GotFocus="DataGridCell_GotFocus">
我添加了以下代码,它运行良好:
private void DataGridCell_GotFocus(object sender, RoutedEventArgs e)
{
DataGridCell cell = e.OriginalSource as DataGridCell;
if (cell != null)
{
// clear existing selection
((DataGrid)sender).SelectedIndex = -1;
// set cell to edit mode
if (cell.Column.DisplayIndex != 0)
{
((DataGrid)sender).BeginEdit(e);
}
// set focus on inner control
Control control = FindFirstChildControl(cell);
if (control != null)
{
control.Focus();
}
}
}
private Control FindFirstChildControl(DependencyObject obj)
{
Control result = null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject childObj = VisualTreeHelper.GetChild(obj, i);
if (childObj != null)
{
if (childObj is Control)
{
result = childObj as Control;
break;
}
else
{
result = FindFirstChildControl(childObj);
if (result != null)
{
break;
}
}
}
}
return result;
}
但是,添加这段代码会使 "shift + tab"(反向制表符)键盘热键不再适用于数据网格。
我如何在拥有 "single click edit" 功能的情况下修复它?
谢谢!
解决此问题的一种方法(可能不是最好的方法)是像这样手动处理 "Shift is down":
private void DataGridCell_GotFocus(object sender, RoutedEventArgs e)
{
// add this line
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) return;
....
}
//and this event handler
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
((DataGrid)sender).CommitEdit();
}
我有一个可编辑的数据网格,我想在数据网格单元格上实现 "single-click edit",这意味着您只需单击一次即可使数据网格单元格可编辑。
<DataGrid RowHeaderWidth="0" BorderThickness="1,1,1,0" GridLinesVisibility="All"
AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserDeleteRows="False"
EnableColumnVirtualization="False" EnableRowVirtualization="False" SelectionMode="Single"
ItemsSource="{Binding Path=Queries}"
SelectedIndex="{Binding Path=SelectedQueryIndex}"
DataGridCell.GotFocus="DataGridCell_GotFocus">
我添加了以下代码,它运行良好:
private void DataGridCell_GotFocus(object sender, RoutedEventArgs e)
{
DataGridCell cell = e.OriginalSource as DataGridCell;
if (cell != null)
{
// clear existing selection
((DataGrid)sender).SelectedIndex = -1;
// set cell to edit mode
if (cell.Column.DisplayIndex != 0)
{
((DataGrid)sender).BeginEdit(e);
}
// set focus on inner control
Control control = FindFirstChildControl(cell);
if (control != null)
{
control.Focus();
}
}
}
private Control FindFirstChildControl(DependencyObject obj)
{
Control result = null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject childObj = VisualTreeHelper.GetChild(obj, i);
if (childObj != null)
{
if (childObj is Control)
{
result = childObj as Control;
break;
}
else
{
result = FindFirstChildControl(childObj);
if (result != null)
{
break;
}
}
}
}
return result;
}
但是,添加这段代码会使 "shift + tab"(反向制表符)键盘热键不再适用于数据网格。
我如何在拥有 "single click edit" 功能的情况下修复它?
谢谢!
解决此问题的一种方法(可能不是最好的方法)是像这样手动处理 "Shift is down":
private void DataGridCell_GotFocus(object sender, RoutedEventArgs e)
{
// add this line
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) return;
....
}
//and this event handler
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
((DataGrid)sender).CommitEdit();
}