【问题标题】:WPF DataGridComboBoxColumn Edit on Key pressWPF DataGridComboBoxColumn 按键编辑
【发布时间】:2017-02-15 21:05:09
【问题描述】:

我有一个带有DataGridComboBoxColumn 的数据网格。

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

我怎样才能让DataGridComboBoxColumn 进入编辑模式而不需要按 F2?理想情况下在 Key Press 上,但如果它也可以在焦点上进入编辑模式,我会很好。

解决方案 修改已接受的答案以恢复数据网格的基本功能:

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

【问题讨论】:

    标签: wpf combobox datagrid keypress


    【解决方案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;
    }
    

    如果这能解决您的问题。

    【讨论】:

    • 这几乎可以工作,我不得不对其进行修改,使其忽略输入、转义和制表键。此外,现在用户必须按两次回车键才能进入下一行......试图找出如何阻止它。
    • @MrZander 这是一个单独问题的候选人。所以请你单独询问。如果这回答了您当前的问号,请相应地标记。
    • 当然,这确实会导致它进入编辑模式,但它破坏了数据网格的几乎所有其他功能。不太理想。无论如何,我已经通过修复问题的修改更新了问题。
    • @MrZander 如果您尝试修改像 DataGrid 这样极其复杂的控件的默认行为,那么您必须做很多事情。
    【解决方案2】:

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

    如果您不希望在其他列上出现这种行为,您可以检查 e.AddedCells[0].Column 属性(如果您的 SelectionUnit="Cell"DataGrid)。

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

    【讨论】:

      【解决方案3】:

      我的回答可能有点晚了,但我想补充一下,因为在不破坏正常 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”似乎不会受到伤害。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-26
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        • 2010-12-15
        • 2013-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多