【问题标题】:Select first item (in list) in a DataGrid on Key.Down event在 Key.Down 事件的 DataGrid 中选择第一项(在列表中)
【发布时间】:2018-09-23 01:32:59
【问题描述】:

当用户按下箭头键时,我正在尝试选择 Datagrid 中的第一行,即 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 时,我需要得到保护,不要一直选择同一行..

【问题讨论】:

    标签: c# wpf datagrid row cell


    【解决方案1】:

    假设您的意思是使用向下箭头键,您的选择非常糟糕。 采用任何数据网格。
    单击一行。
    按向下箭头。
    焦点移至下一行。

    【讨论】:

    • 要点是我不能使用鼠标.. 只需用键盘选择一行并用键盘在数据网格中移动...
    • 您可以直接进入数据网格并按向下箭头。没有代码。
    • 我不能使用选项卡,只需打开新窗口,加载产品,按下键并开始“跨行”..:/
    • 可以在代码中发出遍历请求。 stackoverflow.com/questions/809382/… 推迟使用来自 window 的 contentrendered 事件处理程序的 dispatcher.invokeasync。
    【解决方案2】:

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

    您可以使用视图模型和数据绑定来执行此操作(如果您有兴趣,我可以附加此答案的先前版本),但这并不难。

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

    【讨论】:

    • 我现在正在开车,但我一回家就试一试,非常感谢您的回复!这是一个很好的你正在与 WPF 战斗。你不会赢。 :P“你不能通过!” :D
    • 当我在开车和阅读代码时,也许我错过了一些东西,但我不知道你在哪里告诉程序在按下箭头时选择一行?跨度>
    • @Roxy'Pro 您可能不应该开车和阅读代码。你可能会错过一个行人。我没有包括那部分——我只是说“获取视图模型并在Items 集合中的项目上设置属性”——这部分很简单。
    • @Roxy'Pro 不管怎样,别管那些样板文件,有一个更简单的方法。
    猜你喜欢
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2014-12-06
    • 1970-01-01
    相关资源
    最近更新 更多