Search code examples
wpfcomboboxdatagridkeypress

WPF DataGridComboBoxColumn Edit on Key press


I have a datagrid with a DataGridComboBoxColumn in it.

I want my users to be able to enter edit mode by just typing.
This is the default behavior for DataGridTextColumn and I don't like that they have to press F2 in order to enable editing for just this column type.

How can I make the DataGridComboBoxColumn enter edit mode without them needing to press F2? Ideally on Key Press, but I would be fine if it entered edit mode on focus as well.

Solution Modifications to the accepted answer that bring back the basic functionality of the 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();
            }
        }
    }

Solution

  •     <DataGrid.CellStyle>
             <Style TargetType="DataGridCell">
                 <EventSetter Event="PreviewKeyDown" Handler="Cell_PreviewKeyDown"/>
                 <EventSetter Event="GotFocus" Handler="Cell_GotFocus"/>
             </Style>
        </DataGrid.CellStyle>
    

    Handlers :

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

    Helper :

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

    If this solves your problem.