c# - MVVM DataGrid在刷新内容后如何设置焦点

标签 c# wpf mvvm datagrid

我有一个像下面的数据网格:

<DataGrid ItemsSource="{Binding GridSource}" CanUserAddRows="False" AutoGenerateColumns="False" SelectedItem="{Binding SelectedRow, Mode=TwoWay}">
  ...
</DataGrid>

我在ViewModel中有一个计时器来刷新DataGrid的内容,在更新内容后,使用SelectedRow重置当前选择行。
检查UI结果,选定的行有效,但是没有蓝色背景。我想我错过了设定的重点,但是如何实现呢?

最佳答案

I guess I miss set focus, but how to implement this?



以编程方式将一行集中在DataGrid中,并获得与使用鼠标单击该行来选择行时完全相同的行为,这需要一些努力。有关详细信息,请参阅以下博客文章。

如何在WPF中以编程方式选择和突出显示DataGrid中的行或单元格: https://blog.magnusmontin.net/2013/11/08/how-to-programmatically-select-and-focus-a-row-or-cell-in-a-datagrid-in-wpf/
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();
    /* set the SelectedItem property */
    object item = dataGrid.Items[rowIndex]; // = Product X
    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)
    {
        DataGridCellsPresenter presenter = FindVisualChild<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<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;
}

关于c# - MVVM DataGrid在刷新内容后如何设置焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49996154/

相关文章:

c# - 如何在单个 NuGet 包中发送多个 `dotnet new` 模板?

c# - 如何使用 SqlParameterCollection?

wpf - 设置 WPF/XAML 窗口客户区大小

backbone.js - YII HTML 渲染

silverlight - 'Self' 构造在 Silverlight/MVVM 中有用吗?

c# - 从 C++ 访问 .Net 类?

c# - dot42/xamarin Android编程及应用AlertDialog的SetOnCancelListener方法

c# - 在 Caliburn Micro 中进行多个窗口的正确方法?

c# - WPF MergedDictionaries - 值不能为 Null - 参数名称 : item

c# - 具有部分加载对象的领域模型