【问题标题】:Scrolling wpf datagrid to particular row and focus that row将 wpf 数据网格滚动到特定行并聚焦该行
【发布时间】:2019-05-31 14:34:02
【问题描述】:

我的要求是滚动 wpf 数据网格并到达特定行并关注该行

我正在使用下面的代码来实现它。滚动到特定行工作顺利,但几次焦点不起作用

  private void FocusNextRow()
  {
             int index = indexOfRowsToHighlight[indexOfRowToFocus];
             MessagesDataGrid.SelectedItem = MessagesDataGrid.Items[index];
             MessagesDataGrid.ScrollIntoView(MessagesDataGrid.SelectedItem);
             Thread.Sleep(200);
             MessagesDataGrid.SelectionUnit = DataGridSelectionUnit.FullRow;
             DataGridRow row = MessagesDataGrid.ItemContainerGenerator.ContainerFromIndex(index)  as DataGridRow;
             DataGridCell cell = GetCell(MessagesDataGrid, row, 0);
             cell.Focus();
   }
   private static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
   {
        if (rowContainer != null)
        {
              DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);              
              // try to get the cell but it may possibly be virtualized
              DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
              if (cell == null)
              {                                  
                  cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
              }
               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;
        }

在 100 次 90 次中,焦点有效,但有几次随机无效

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    它可能是虚拟化! 调用 FocusNextRow 函数时,检查 ItemContainerGenerator 的状态

    MessagesDataGrid.ItemContainerGenerator.ContainerFromIndex(index)  as DataGridRow;
    

    https://docs.microsoft.com/ko-kr/dotnet/api/system.windows.controls.itemcontainergenerator?view=netframework-4.8

    【讨论】:

    • 我调试了我的代码,发现对于 VisualTreeHelper.GetChildrenCount(obj) = 0 的单元格,它没有聚焦,所以我的下一个问题是我如何明确传达给 VisualTreeHelper 以查找单元格
    • 它应该以不同的方式实现。例如,创建自定义 DataGridCell 控件并检查 IsVisible 属性或引发 VisibleChanged 事件时,聚焦!
    【解决方案2】:

    我为这个问题找到了两个解决方案 1.禁用数据网格的行虚拟化属性。但它会严重影响性能。 2. 检查 FindVisualChild() 是否能够找到行容器,如果没有,则在该场景中调用 rowcontainer.ApplyTemplate() 方法,该方法 ApplyTemplate() 确保在可视化树中创建您的行。更有效的做法

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多