We are using XamDataGrid in our application using MVVM pattern and allowing user to select only one row at a time. When user selects a row, two buttons above the Grid will get enabled. This is the functionality which we need.
So, when user selects Ctrl button on keyboard and clicks on left button on the same selected row, the buttons are getting disabled. This is because the selected row is getting deselected. Is there any way to prevent this ?
I have tried to handle this through MouseDown event but that doesn't work as the SelectedDataItem property of the Grid is updating by the time this event gets hit.
Please provide a solution to handle this scenario.
Hello,
Thank you for the feedback.
In order to remove the highlight of the record by holding down the Ctrl key and clicking the record, I can suggest you a couple of approaches, depending on the exact functionality you are looking for.
private void DataRecordPresenter_PreviewMouseDown(object sender, MouseButtonEventArgs e){ var drp = sender as DataRecordPresenter; if (drp.IsSelected && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) { // (Approach 1) Unselect the record (removes entire highlight and deselects the record) //drp.IsSelected = false; // (Approach 2) Remove the highlight only and keep the selection. (the record will remain selected without any indication for being selected) drp.IsActive = false; var drca = Utilities.GetDescendantFromType(drp, typeof(DataRecordCellArea), true) as DataRecordCellArea; drca.BackgroundSelected = Brushes.Transparent; drca.BorderSelectedBrush = Brushes.Transparent; e.Handled = true; }}
if (drp.IsSelected && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) { // (Approach 1) Unselect the record (removes entire highlight and deselects the record) //drp.IsSelected = false;
// (Approach 2) Remove the highlight only and keep the selection. (the record will remain selected without any indication for being selected) drp.IsActive = false; var drca = Utilities.GetDescendantFromType(drp, typeof(DataRecordCellArea), true) as DataRecordCellArea; drca.BackgroundSelected = Brushes.Transparent; drca.BorderSelectedBrush = Brushes.Transparent;
e.Handled = true; }}
If you have any questions, please let me know.
Sorry for the delay and thank you for detailed explanation Tacho. The suggested solution has worked :)
Testing is in progress, I will get back to you if I found any issue.
I have another question : is it possible to un-highlight the row when user does Ctrl + clicking the selected row. This clears the user that row has unselected.
Thank you for the support.
Thank you for the detailed description of the record selection behavior you are using.
In order to prevent an already selected record from getting deselected when clicking it while holding the Ctrl key down, I can suggest you handle the tunneling PreviewMouseDown event.Inside the handler we can mark the event as handled (e.Handled = true;) by simply checking if the record is already selected and if the Left/Right Ctrl key is pressed.Since the logic is View specific, it will keep the MVVM pattern intact.
XAML:
<igDP:XamDataGrid.Resources> <Style TargetType="igDP:DataRecordPresenter"> <EventSetter Event="PreviewMouseDown" Handler="DataRecordPresenter_PreviewMouseDown" /> </Style></igDP:XamDataGrid.Resources>
Code-behind:
private void DataRecordPresenter_PreviewMouseDown(object sender, MouseButtonEventArgs e){ var drp = sender as DataRecordPresenter; if (drp.IsSelected && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) e.Handled = true; }
if (drp.IsSelected && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) e.Handled = true; }
I have attached a sample application that demonstrates the approach from above.