按键上的WPF DataGridComboBoxColumn编辑

时间:2016-10-07 00:16:17

标签: wpf combobox datagrid keypress

我有一个数据网格,其中包含DataGridComboBoxColumn

我希望我的用户只需输入即可进入编辑模式 这是DataGridTextColumn的默认行为,我不喜欢他们必须按F2才能启用此列类型的编辑。

如何让DataGridComboBoxColumn进入编辑模式而不需要按F2?理想情况下,按键,但如果它也在焦点上进入编辑模式,我会没事的。

解决方案 对已接受的答案进行修改,以恢复datagrid的基本功能:

 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();
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

您可以尝试使用SelectedCellsChanged事件。它适用于焦点变化。

如果您不想在其他列上执行此操作,则可以检查e.AddedCells[0].Column属性(如果SelectionUnit="Cell"DataGrid)。

private void dgTest_SelectedCellsChanged( object sender, SelectedCellsChangedEventArgs e )
{
    ( sender as DataGrid ).BeginEdit();
}

答案 1 :(得分:1)

    <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;
}

如果这样可以解决您的问题。

答案 2 :(得分:1)

我的回答可能有点晚了,但是我想补充一下,因为没有给定的答案可以在不破坏正常的DataGrid键盘处理的情况下为我解决问题。

问题很简单:

我希望我的用户只需键入即可进入编辑模式。

解决方案也是这样:

<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();
    }
}

因为这已经是TextBoxes的正常行为,所以我只想将其应用于DataGridComboBoxColumn。当然,每次按键都将调用此函数,但是当DataGrid已处于编辑模式时,对“ BeginEdit”的调用似乎不会受到损害。

相关问题