简体   繁体   English

将wpf datagrid滚动到特定行并聚焦该行

[英]Scrolling wpf datagrid to particular row and focus that row

My requirement is to scroll through wpf data grid and reach to particular row and focus that row 我的要求是滚动wpf数据网格并到达特定行并聚焦该行

I am using the below code to achieve it . 我使用下面的代码来实现它。 Scrolling to particular row is working smoothly but few times focus not work 滚动到特定行正在顺利进行,但很少有人关注不起作用

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

Out of 100 times 90 times focus is working but few times randomly it does not work 在100次90次焦点工作,但几次随机它不起作用

it's maybe virtualizing!! 它可能是虚拟化! when call FocusNextRow function, check status of ItemContainerGenerator 当调用FocusNextRow函数时,检查ItemContainerGenerator的状态

MessagesDataGrid.ItemContainerGenerator.ContainerFromIndex(index)  as DataGridRow;

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

I find two solution for this problem 1. Disable row virtualization property of data grid . 我找到了两个解决此问题的方法1.禁用数据网格的行虚拟化属性。 But its seriously going to impact the performance. 但它严重影响了性能。 2. Check whether FindVisualChild() ables to find the row container or not , if not then in that scenario calls rowcontainer.ApplyTemplate() method , this method ApplyTemplate() ensures your row is created in the visual tree. 2.检查FindVisualChild()是否能够找到行容器,如果没有,则在该场景中调用rowcontainer.ApplyTemplate()方法,此方法ApplyTemplate()确保在可视树中创建行。 its more efficient way of doing 它更有效的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM