我在WPF中有DataGrid
。在WPF DataGrid中使用Enter键作为选项卡
我想移动到NextCell时,我打输入,并在达到LastColumn时,它应该有默认的输入创建或移动到下一行的功能。
我不想使用标签
我怎么能在WPF做到这一点。
我在WPF中有DataGrid
。在WPF DataGrid中使用Enter键作为选项卡
我想移动到NextCell时,我打输入,并在达到LastColumn时,它应该有默认的输入创建或移动到下一行的功能。
我不想使用标签
我怎么能在WPF做到这一点。
试试这个我认为它的工作至少为我工作。
//datagrid gotfocus event
private void dataGrid1_GotFocus(object sender, RoutedEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
//here we just find the cell got focused ...
//then we can use the cell key down or key up
// iteratively traverse the visual tree
while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
//raise key down event of cell
cell.IsSelected = true;
cell.KeyDown += new KeyEventHandler(cell_KeyDown);
}
}
void cell_KeyDown(object sender, KeyEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (e.Key == Key.Enter)
{
cell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
cell.IsSelected = false;
e.Handled = true;
cell.KeyDown -= cell_KeyDown;
}
}
在这段代码中,当一个单元格获得焦点并且用户键入下一个单元格时将获得焦点。 祝你好运希望这可以帮助你。
编辑:
设置此功能的DataGrid PreviewKeyDown事件。
private void maindg_PreviewKeyDown(object sender, KeyEventArgs e)
{
//just accept enter key
if (e.Key != Key.Enter) return;
DependencyObject dep = (DependencyObject)e.OriginalSource;
//here we just find the cell got focused ...
//then we can use the cell key down or key up
// iteratively traverse the visual tree
while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridCell)
{
//cancel if datagrid in edit mode
maindg.CancelEdit();
//get current cell
DataGridCell cell = dep as DataGridCell;
//deselect current cell
cell.IsSelected = false;
//find next right cell
var nextCell = cell.PredictFocus(FocusNavigationDirection.Right);
//if next right cell null go for find next ro first cell
if (nextCell == null)
{
DependencyObject nextRowCell;
nextRowCell = cell.PredictFocus(FocusNavigationDirection.Down);
//if next row is null so we have no more row Return;
if (nextRowCell == null) return;
//we do this because we cant use FocusNavigationDirection.Next for function PredictFocus
//so we have to find it this way
while ((nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left) != null)
nextRowCell = (nextRowCell as DataGridCell).PredictFocus(FocusNavigationDirection.Left);
//set new cell as next cell
nextCell = nextRowCell;
}
//change current cell
maindg.CurrentCell = new DataGridCellInfo(nextCell as DataGridCell);
//change selected cell
(nextCell as DataGridCell).IsSelected = true;
// start edit mode
maindg.BeginEdit();
}
//handl the default action of keydown
e.Handled = true;
}
其不工作 – 2012-05-07 21:43:28
@Mamad R,它的工作,我有一个需求,当下一个单元格获得焦点时如何使它可编辑也 – Mussammil 2013-12-13 04:39:54
@Mussammil阅读我的最后编辑。希望能帮到你:) – 2013-12-15 07:50:12
一个简单得多的实现。这个想法是捕获keydown事件,如果键是“Enter”,则移动到下一个选项卡,该选项卡是网格的下一个单元格。
/// <summary>
/// On Enter Key, it tabs to into next cell.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
var uiElement = e.OriginalSource as UIElement;
if (e.Key == Key.Enter && uiElement != null)
{
e.Handled = true;
uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
你有什么试过的?这听起来像是很容易附加到'KeyUp'事件并处理一个''按... –
Tejs
2012-04-23 20:32:09
但它不工作 – 2012-04-23 20:32:48
dgrow.MoveFocus(新的TraversalRequest(FocusNavigationDirection.Next)); – 2012-04-23 20:33:12