【问题标题】:C# WPF KeyDown highlight + selected last row in datagridC# WPF KeyDown 高亮+选中数据网格中的最后一行
【发布时间】:2017-07-23 14:30:05
【问题描述】:

我正在开发用户可以将产品添加到数据网格的应用程序,其中包含产品名称和价格等简单信息,然后是 例如,我想按键盘上的 F4 键,我想专注于数据网格中的最后一项,这意味着选择它并突出显示该项目!

所以伙计们,我怎么能做到这一点,我尝试了一些解决方案,比如将选定的索引设置为我的数据网格和类似的东西,但它不起作用

谢谢各位, 干杯

【问题讨论】:

  • 你有示例代码吗?您使用什么代码来设置选定的索引?你尝试的哪一部分没有成功?请写一个Minimal, Complete, and Verifiable 的例子来回答你的问题。

标签: c# wpf select datagrid highlight


【解决方案1】:

您可以使用 InputBinding 来识别按下的 F4 键。

<Window.InputBindings>
    <KeyBinding Key="F4"
                Command="{Binding SelectLastItemCommand}" />
</Window.InputBindings>

您可以在这里查看如何选择项目:WPF Binding SelectedItem in DataGrid

【讨论】:

    【解决方案2】:

    您的问题出在哪里?处理按钮事件或突出显示行?它似乎是后者,所以看看这个: https://www.codeproject.com/Tips/773382/Row-Highlighting-in-WPF-Grids

    【讨论】:

      【解决方案3】:

      以编程方式突出显示DataGrid 中的行或单元格比仅设置SelectedIndexSelectedItem 属性要复杂一些。

      然而,通过访问DataGrid 控件的可视用户界面元素并在特定@987654327 上调用UIElement.Focus() 方法,可以在代码中选择并聚焦一行并获得与使用鼠标时相同的行为@ 对象,如以下博客文章中所述。

      如何在 WPF 的 DataGrid 中以编程方式选择并聚焦一行或单元格: https://blog.magnusmontin.net/2013/11/08/how-to-programmatically-select-and-focus-a-row-or-cell-in-a-datagrid-in-wpf/

      这是一个例子:

      public partial class MainWindow : Window
      {
          public MainWindow
          {
              InitializeComponent();
              this.PreviewKeyDown += (s, e) => 
              {
                  if(e.Key == Key.F4)
                      SelectRowByIndex(dataGridProducts, dataGridProducts.Items.Count - 1);
              };
      
              //populate DataGrid etc...
          }
      
          private static void SelectRowByIndex(DataGrid dataGrid, int rowIndex)
          {
              if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
                  throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow.");
      
              if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
                  throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));
      
              dataGrid.SelectedItems.Clear();
              object item = dataGrid.Items[rowIndex];
              dataGrid.SelectedItem = item;
      
              DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
              if (row == null)
              {
                  /* bring the data item (Product object) into view
                   * in case it has been virtualized away */
                  dataGrid.ScrollIntoView(item);
                  row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
              }
              if (row != null)
              {
                  DataGridCell cell = GetCell(dataGrid, row, 0);
                  if (cell != null)
                      cell.Focus();
              }
          }
      
          private static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
          {
              if (rowContainer != null)
              {
                  System.Windows.Controls.Primitives.DataGridCellsPresenter presenter 
                      = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
                  if (presenter == null)
                  {
                      /* if the row has been virtualized away, call its ApplyTemplate() method 
                       * to build its visual tree in order for the DataGridCellsPresenter
                       * and the DataGridCells to be created */
                      rowContainer.ApplyTemplate();
                      presenter = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
                  }
                  if (presenter != null)
                  {
                      DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                      if (cell == null)
                      {
                          /* bring the column into view
                           * in case it has been virtualized away */
                          dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
                          cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                      }
                      return cell;
                  }
              }
              return null;
          }
      
          private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
          {
              for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
              {
                  DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                  if (child != null && child is T)
                      return (T)child;
                  else
                  {
                      T childOfChild = FindVisualChild<T>(child);
                      if (childOfChild != null)
                          return childOfChild;
                  }
              }
              return null;
          }
      }
      

      【讨论】:

      • 你是这个游戏的神吗?你真是个了不起的人!多谢!你怎么知道这些东西?
      • 经验为王 :)
      • 我会投票给任何你想要的人!您是否有时可以在您的代码中编写 cmets(即使您在发布代码之前编写它)例如您可以在代码行上方编写 cmets,即您为什么这样做以及为什么这样做,它比您的代码更容易理解!再一次非常感谢你! :)
      • 我做到了,不是吗?
      • 是的,先生,您在这里做到了!我正在谈论更多关于以前的问题,您还发布了很棒的答案!这是关于从可观察的集合中删除项目等,并且您提供了完美的解决方案,但我在理解它时遇到了一点困难,但之后就可以了!我之所以这么说,是因为很容易看出你是一个真正有经验的人,所以你的每一句话对我们所有人(没有那么多经验的年轻程序员)都非常有帮助。再次非常感谢!!
      猜你喜欢
      • 2011-07-27
      • 1970-01-01
      • 2013-08-31
      • 2012-09-05
      • 2017-12-13
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多