WPF DataGridComboBoxColumn 按键编辑
WPF DataGridComboBoxColumn Edit on Key press
我有一个包含 DataGridComboBoxColumn
的数据网格。
我希望我的用户只需键入即可进入编辑模式。
这是 DataGridTextColumn
的默认行为,我不喜欢他们必须按 F2 才能仅针对此列类型启用编辑。
如何让 DataGridComboBoxColumn
无需按 F2 键即可进入编辑模式?理想情况下是按键,但如果它也进入焦点编辑模式我会很好。
解决方案
对已接受的答案进行修改以恢复数据网格的基本功能:
void Cell_PreviewKeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter || e.Key == Key.Tab)
{
dgBins.CommitEdit();
dgBins.SelectedIndex += 1;
}else if(e.Key.ToString().Length == 1
|| (e.Key.ToString().StartsWith("D") && e.Key.ToString().Length == 2)
|| e.Key.ToString().StartsWith("NumPad")
|| e.Key == Key.Delete
|| e.Key == Key.Back )
{
if (e.OriginalSource is DataGridCell)
{
DataGridCell cell = (sender as DataGridCell);
Control elem = FindChild<Control>(cell, null);
elem.Focus();
}
}
}
您可以尝试使用SelectedCellsChanged
事件。它适用于焦点改变。
如果您不希望其他列出现该行为,您可以检查 e.AddedCells[0].Column
属性(如果您的 SelectionUnit="Cell"
或 DataGrid
)。
private void dgTest_SelectedCellsChanged( object sender, SelectedCellsChangedEventArgs e )
{
( sender as DataGrid ).BeginEdit();
}
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewKeyDown" Handler="Cell_PreviewKeyDown"/>
<EventSetter Event="GotFocus" Handler="Cell_GotFocus"/>
</Style>
</DataGrid.CellStyle>
处理程序:
void Cell_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.OriginalSource is DataGridCell)
{
DataGridCell cell = (sender as DataGridCell);
Control elem = FindChild<Control>(cell, null);
elem.Focus();
}
}
void Cell_GotFocus(object sender, RoutedEventArgs e)
{
DataGridCell cell = (sender as DataGridCell);
cell.IsEditing = true;
}
帮手:
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
如果这能解决您的问题。
我的回答可能有点晚了,但我想补充一点,因为没有给出的答案可以在不破坏正常的 DataGrid 键盘处理的情况下为我解决问题。
问题很简单:
I want my users to be able to enter edit mode by just typing.
解决方案也是如此:
<DataGrid KeyDown="DataGrid_KeyDown">
...
</DataGrid>
代码在代码后面xaml.cs:
private void DataGrid_KeyDown(object sender, KeyEventArgs e)
{
DataGrid dg = (sender as DataGrid);
if (dg.CurrentColumn is DataGridComboBoxColumn)
{
dg.BeginEdit();
}
}
因为这已经是 TextBox 的正常行为,所以我只想将其应用于 DataGridComboBoxColumn。当然这会在每次按键时调用,但是当 DataGrid 已经处于编辑模式时调用 'BeginEdit' 似乎没有什么坏处。
我有一个包含 DataGridComboBoxColumn
的数据网格。
我希望我的用户只需键入即可进入编辑模式。
这是 DataGridTextColumn
的默认行为,我不喜欢他们必须按 F2 才能仅针对此列类型启用编辑。
如何让 DataGridComboBoxColumn
无需按 F2 键即可进入编辑模式?理想情况下是按键,但如果它也进入焦点编辑模式我会很好。
解决方案 对已接受的答案进行修改以恢复数据网格的基本功能:
void Cell_PreviewKeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter || e.Key == Key.Tab)
{
dgBins.CommitEdit();
dgBins.SelectedIndex += 1;
}else if(e.Key.ToString().Length == 1
|| (e.Key.ToString().StartsWith("D") && e.Key.ToString().Length == 2)
|| e.Key.ToString().StartsWith("NumPad")
|| e.Key == Key.Delete
|| e.Key == Key.Back )
{
if (e.OriginalSource is DataGridCell)
{
DataGridCell cell = (sender as DataGridCell);
Control elem = FindChild<Control>(cell, null);
elem.Focus();
}
}
}
您可以尝试使用SelectedCellsChanged
事件。它适用于焦点改变。
如果您不希望其他列出现该行为,您可以检查 e.AddedCells[0].Column
属性(如果您的 SelectionUnit="Cell"
或 DataGrid
)。
private void dgTest_SelectedCellsChanged( object sender, SelectedCellsChangedEventArgs e )
{
( sender as DataGrid ).BeginEdit();
}
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewKeyDown" Handler="Cell_PreviewKeyDown"/>
<EventSetter Event="GotFocus" Handler="Cell_GotFocus"/>
</Style>
</DataGrid.CellStyle>
处理程序:
void Cell_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.OriginalSource is DataGridCell)
{
DataGridCell cell = (sender as DataGridCell);
Control elem = FindChild<Control>(cell, null);
elem.Focus();
}
}
void Cell_GotFocus(object sender, RoutedEventArgs e)
{
DataGridCell cell = (sender as DataGridCell);
cell.IsEditing = true;
}
帮手:
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
如果这能解决您的问题。
我的回答可能有点晚了,但我想补充一点,因为没有给出的答案可以在不破坏正常的 DataGrid 键盘处理的情况下为我解决问题。
问题很简单:
I want my users to be able to enter edit mode by just typing.
解决方案也是如此:
<DataGrid KeyDown="DataGrid_KeyDown">
...
</DataGrid>
代码在代码后面xaml.cs:
private void DataGrid_KeyDown(object sender, KeyEventArgs e)
{
DataGrid dg = (sender as DataGrid);
if (dg.CurrentColumn is DataGridComboBoxColumn)
{
dg.BeginEdit();
}
}
因为这已经是 TextBox 的正常行为,所以我只想将其应用于 DataGridComboBoxColumn。当然这会在每次按键时调用,但是当 DataGrid 已经处于编辑模式时调用 'BeginEdit' 似乎没有什么坏处。