如何获取所有 DataGrid 的选定项。 (最新)

发布于 2024-10-12 22:07:02 字数 477 浏览 3 评论 0原文

我目前正在尝试获取用户选择的选定数据网格行的集合(多行选择)。

每行都绑定到一个视觉对象,我想将其显示为选定的“

所以对于数据网格,我添加了这种样式:

<Style TargetType="DataGridRow">
      <EventSetter Event="Selected" Handler="DataGrid_RowSelectionChanged" />
</Style>

因此,当用户选择一行时,我会触发一个事件。

但是,当选择一行并进入事件时。所选项目尚未更新,它仍然显示我之前选择的内容 DataGrid.CurrentItem 显示我刚刚单击并即将选择的行,但由于其多重选择,我希望能够获取当前选择的所有行并。我的视觉对象“突出显示”

有没有办法可以从数据网格中获取最新的所选项目列表? 在我选择新行后是否可以使用该事件

? 凯夫

I am currently trying to get a collection of selected datagrid rows selected by the user (multi row selection on).

Each row is binded to a visual object which i want to show as selected"

So for the data grid I added this style:

<Style TargetType="DataGridRow">
      <EventSetter Event="Selected" Handler="DataGrid_RowSelectionChanged" />
</Style>

So when the user selects a row I get an event fired.

However, when select a row and into the event. Selected item has not been updated yet and it still shows what I PREVIOUSLY selected. DataGrid.CurrentItem shows the row I just clicked and about to be selected, but since its multi select, I want to be able to get ALL the rows CURRENTLY selected and have my visual object "Highlighted"

Is there a way where I can get a MOST updated list of selected item from datagrid? Is there an event I can use that fires AFTER i selected my new row?

Thanks and Regards,
Kev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

药祭#氼 2024-10-19 22:07:02

您想要使用 DataGrid.SelectionChanged 事件。 SelectionChangedEventArgs 将包含在选择更改时添加或删除的项目。

        DataGrid dg = new DataGrid();
        dg.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(OnSelectionChanged);

        void OnSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            foreach (var addedItem in e.AddedItems)
            {
                //do stuff
            }

            foreach (var removedItem in e.RemovedItems)
            {
                //do stuff
            }
        }

You want to use the DataGrid.SelectionChanged event. The SelectionChangedEventArgs will contain the items which were added or removed at the time of selection changing.

        DataGrid dg = new DataGrid();
        dg.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(OnSelectionChanged);

        void OnSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            foreach (var addedItem in e.AddedItems)
            {
                //do stuff
            }

            foreach (var removedItem in e.RemovedItems)
            {
                //do stuff
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文