本文介绍了WPF 数据网格——行的编程选择似乎破坏了多选(特别是 shift-click 多选)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF DataGrid 控件,它的 SelectionUnit 为FullRow",SelectionMode 为Extended",我正在以编程方式在其中选择一个项目(通常是第一个项目).选择有效,但出于某种原因,任何形式的程序化选择似乎都会破坏 shift-select 多选功能.

I have a WPF DataGrid control with a SelectionUnit of "FullRow" and SelectionMode of "Extended" that I'm programmatically selecting an item in (the first item, usually). The selection works, but for some reason any form of programmatic selection seems to break the shift-select multiselect ability.

如果我单击 DataGrid 中的另一个项目(因此我刚刚单击的项目是唯一选定的项目),则 shift-select 将起作用.如果我以编程方式选择了该项目,它似乎只会中断.此外,在任何一种情况下,按住 Control 单击都可以选择多个项目——似乎只有 shift-select 被破坏了.

If I single click another item in the DataGrid (so the item I just clicked is the only item selected), then shift-select will work. It only seems to break if I've programmatically selected the item. Additionally, control-click works to select multiple items in either case -- it seems to only be shift-select that is broken.

我尝试了各种形式的以编程方式选择单个项目的方式,从简单的 myGrid.SelectedIndex = 0 到使用 DataGrid 的 ItemContainerGenerator 来获取 DataGridRow 对象的实例并在其上设置 IsSelected = true,但没有有用.

I've tried various forms of programmatically selecting the single item, from as simple as myGrid.SelectedIndex = 0, to using the DataGrid's ItemContainerGenerator to get an instance of the DataGridRow object and setting IsSelected = true on it, but to no avail.

再次重复 -- 项目的程序化选择有效,但会破坏 shift-click 选择.

To re-iterate -- programmatic selection of an item works, but it breaks shift-click selection.

有没有人遇到过这种情况?我尝试将焦点设置在以编程方式选择的 DataGridRow 实例上,但似乎没有帮助?

Has anyone run into this before? I've tried setting focus on the DataGridRow instance that is programmatically selected, but it doesn't seem to help?

推荐答案

我成功地使用反射解决了这个问题:

I succeeded to work around this problem using reflection:

var method = typeof(DataGrid).GetMethod("HandleSelectionForCellInput", BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(MyDataGrid, new object[] { cellToSelect, false, false, false });

这篇关于WPF 数据网格——行的编程选择似乎破坏了多选(特别是 shift-click 多选)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 06:22