Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
225
How to prevent deselect in full-row single-select mode using ctrl + click selected row
posted

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.

Parents
No Data
Reply
  • 6365
    Offline posted

    Hello,

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

    I have attached a sample application that demonstrates the approach from above.

    If you have any questions, please let me know.

    XamDataGrid_sample.zip
Children