在Key.Down事件的DataGrid中选择第一个项目(在列表中)

时间:2018-04-12 19:17:40

标签: c# wpf datagrid row cell

我正在尝试在我的数据网格中选择第一行,当用户按下箭头键时,Key.Down事件。

它现在正常工作,但即使我传递索引[0],它也会选择第二行......

我创建了方法SelectRowByIndex,它应该选择我的Datagrid的第一行,它看起来像这样:

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)
            {
                //Moram dodati BillItemTemp u slučaju da je 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)
                {

                    rowContainer.ApplyTemplate();
                    presenter = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
                }
                if (presenter != null)
                {
                    DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                    if (cell == null)
                    {

                        dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
                        cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                    }
                    return cell;
                }
            }
            return null;
        }

之后我在构造函数中调用它时加载了表单:

 this.PreviewKeyDown += (s, e) =>
 {
        if (e.Key == Key.Down && dtgProducts.HasItems)
          SelectRowByIndex(dtgProducts, 0);
 };

但不知怎的,它正在选择第二排?而不是第一个...怎么来的?

当我一直按下Key.Down而不是一直选择同一行时,我需要得到保护..

2 个答案:

答案 0 :(得分:0)

你做出了一个非常糟糕的选择,假设你的意思是向下箭头键。 拿任何数据网格 点击一行。
按向下箭头 焦点移动到下一行。

答案 1 :(得分:0)

你正在与WPF作战。你不会赢。当你以这种方式做事时,WPF并不喜欢它。它通常希望您使用数据绑定。

你可以用viewmodels和数据绑定来做到这一点(如果你感兴趣,我可以追加这个答案的先前版本),但它甚至不那么难。

private static void SelectRowByIndex(DataGrid dataGrid, int rowIndex)
{
    //  Or set this in XAML better yet
    dataGrid.IsSynchronizedWithCurrentItem = true;

    var view = CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);

    view.MoveCurrentToPosition(rowIndex);
}