Hi
I would like to select a record upon mouse right click. The data grid is setup for extended selection mode and I'm able to select record on right click if my data grid doesn't have any preselected rows. However, it behaves differently when already one or more records selected in it. Please help me with this issue. Sample code provided below:
xaml
<igDataPresenter:XamDataGrid.Resources> <Style TargetType="{x:Type igDataPresenter:DataRecordPresenter}"> <EventSetter Event="MouseRightButtonDown" Handler="XamDataGrid_OnMouseRightButtonDown"/> </Style> </igDataPresenter:XamDataGrid.Resources>
xaml.cs
private void XamDataGrid_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e) { var viewModel = DataContext as StructureViewModel; if (viewModel != null) { var dataRecordPresenter = sender as DataRecordPresenter; if (dataRecordPresenter != null && dataRecordPresenter.Record != null && dataGrid != null) {dataRecordPresenter.Record.IsSelected = true; } } }
Thanks
Hello,
Thank you for your post.
I believe what may be happening here is that by using the dataRecordPresenter.Record.IsSelected = true code, you are essentially setting that record to be the only selected record in the XamDataGrid, and you are clearing the XamDataGrid's SelectedItems collection of all records except that one.
I would recommend that instead of setting the record's IsSelected property, that you use the following code for the selection:
dataRecordPresenter.DataPresenter.SelectedItems.Records.Add(dataRecordPresenter.Record);
This will simply add the record to the selected items collection, rather than clearing the collection and setting that record as the only selected one.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewDeveloper Support Engineer IInfragistics Inc.www.infragistics.com/support
Thanks Andrew. I want to select only the record that I right clicked, all other selected records should be deselected. It was not happening and was showing them as selected.
From your suggestion, I tried to write some code to deselect the existing selected records before selecting the newly right clicked row. but it still have the same behavior, i.e. still shows already selected record and as well as right click selected record.
xaml: NO changes
private void XamDataGrid_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{ var dataRecordPresenter = sender as DataRecordPresenter; if (dataRecordPresenter != null && dataRecordPresenter.Record != null) { foreach (var record in dataRecordPresenter.DataPresenter.SelectedItems.Records) { record.IsSelected = false; } dataRecordPresenter.DataPresenter.SelectedItems.Records.Add(dataRecordPresenter.Record);
} }
Please help.
Thanks,
Sreeni