开发者

How to get ALL DataGrid's selected Item. (MOST up to date)

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 u开发者_如何学运维se that fires AFTER i selected my new row?

Thanks and Regards, Kev


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
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜