How do i get right mouse click to add active record to SelectedItems collection without removing currently selected records?
This is what I currently have but records are being unselected on right mouse click:
void OnGridMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DataRecordPresenter drp = sender as DataRecordPresenter;
if (drp != null)
if (drp.DataPresenter != null
&& drp.DataPresenter.SelectedItems != null
&& drp.DataPresenter.SelectedItems.Records != null)
drp.DataPresenter.SelectedItems.Records.Add(drp.Record);
}
TIA.
You can use the PreviewMouseRightButtonDown event and the following code to obtain the DataRecordPresenter:
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings SelectionTypeRecord="Single"/>
</igDP:XamDataGrid.FieldLayoutSettings>
private void xamDataGrid1_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
DependencyObject source = e.OriginalSource as DependencyObject;
if (source == null) return;
DataRecordPresenter drp =
Infragistics.Windows.Utilities.GetAncestorFromType(source, typeof(DataRecordPresenter), true) as DataRecordPresenter;
if (drp == null) return;
if (drp.Record != null)
drp.Record.IsSelected = true;
drp.IsActive = true;
Hope this helps!
Vlad, thanks for response.
Your proposed solution selects the current active record upon mouse right click, which is good. However, it does not solve the problem where currently selected items are unselected upon mouse right click.
The following change should do the trick:
drp.Record.IsSelected = !drp.Record.IsSelected;
No, that is not what I am looking for.
If I selected multiple records using CTRL + left mouse click and then right mouse click on the grid. previously selected records are unselected, while the active record is selected. I want active record to get selected, which is what you proposed, and previously selected records to stay selected .
Vlad, any new suggestions for this request/
Please, find the attached sample that seems to work fine on my side.
When you run the sample, you can use the Ctrl key and the mouse left button to select the first three records for example. Then, when you right click on another record it is added to the selected records collection, keeping the previous selection . If you need to clear the last selected records then you can use the Clear method on the SelectedItems.Records collection as follows:
xdg1.SelectedItems.Records.Clear();
Let me know if this is what you are looking for.
Vlad, this sample does not do what you describe. If I select multiple records with Ctrl + click and right click on another record, previously selected records are unselected but right clicked record is selected. However, If I continue to right click, i keep adding records to the selected collection.
Also, if I select multiple records using Ctrl + click, I do not see these in the "Selected Records" listbox.
Found my problem.. Registered event handler using EventManager twice and in first case, was clearing selected items collection. so this has been all my fault since first suggestion. I apologize for the causing the confusion.
I slightly modified sample and it is doing what I want. Added CellClickAction of SelectRecord and
changed handler as you originally proposed without toggling Selected state. So need to figure out why it is not doing this in my app.
Thanks.