1

在刷新有界集合的数据后,我们有一个焦点问题,即聚焦 DataGrid 的一个单元格。例如,我们为其集合设置一个过滤器,然后我们想要重新聚焦存储列的存储单元格。

我们是否认为对 ScrollIntoView 的调用是同步的,这是否意味着在调用它之后创建了我们想要的行和单元格并且我们可以设置焦点?(这再次意味着在我们调用 ScrollIntoView 之后,我们是否认为 Itemsgenerator 完成了它的工作并且我们可以找到我们想要的单元格)

$

   //set filter of DataGrid Collection
DataGrid_Standard.ScrollIntoView(rowNumber,cellNumber);
//we sure our desired cell are created now
    DataGridRow row =           (DataGridRow)DataGrid_Standard.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // may be virtualized, bring into view and try again
        DataGrid_Standard.ScrollIntoView(DataGrid_Standard.Items[index]);
        row = (DataGridRow)DataGrid_Standard.ItemContainerGenerator.ContainerFromIndex(index);
    }


        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

        // try to get the cell but it may possibly be virtualized
        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);

            // now try to bring into view and retreive the cell
            DataGrid_Standard.ScrollIntoView(rowContainer, DataGrid_Standard.Columns[column]);
            cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);             cell.focus();

有关的

4

2 回答 2

0
Action action = () =>
      {
        dg .ScrollIntoView(dg .SelectedItem);

        var item = dg.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
        if (item == null) return;

        item.Focus();
      };

      Dispatcher.BeginInvoke(DispatcherPriority.Background, action);

这应该适用于您的情况。

于 2014-04-10T14:09:33.473 回答
0

这是一个数据网格选择更改事件处理程序,它将虚拟化行移动到视图中,然后将焦点设置到该行。这对我有用:

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dg = (DataGrid)sender;
        if (dg.SelectedItem == null) return;
        dg.ScrollIntoView(dg.SelectedItem);

        DataGridRow dg_row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromItem(dg.SelectedItem);
        if (dg_row == null) return;
       dg_row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

    }

编辑:使用 dg_row.MoveFocus 方法产生了不可取的效果(复选框列需要两次单击来设置而不是一次),并且它对我来说效果更好,只需使用

dg_row.Focus();
于 2011-10-25T00:16:42.473 回答