Deselect Row in Single Selection Mode

Per the following thread...

https://www.syncfusion.com/forums/146939/how-do-i-de-select-a-row-after-its-selected

It appears that you are considering the ability to allow deselection when selection mode is single. We are on the current version (20). Is this planning to be implemented soon?

If not, how does one implement this in an MVVM architecture?


4 Replies

DM Dhanasekar Mohanraj Syncfusion Team April 21, 2022 03:34 PM UTC

Hi Clay Hess,

As we mentioned earlier, we will implement this feature in any of our upcoming release based on feasibilities and other parameters. Meanwhile, you can achieve your requirement using behavior like below,

C#:

this.AssociatedObject.PreviewMouseUp += AssociatedObject_PreviewMouseUp;

private void AssociatedObject_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)

{

    var rowIndex = window.dataGrid.GetVisualContainer().PointToCellRowColumnIndex(e.GetPosition(window.dataGrid.GetVisualContainer())).RowIndex;

    var record = window.dataGrid.GetRecordAtRowIndex(rowIndex);

    if (window.dataGrid.SelectedItems.Contains(record))

        window.dataGrid.SelectedItem = null;

}


We have created the sample based on your requirement. Please check this and revert us if you need further assistance for this.

Regards,
Dhanasekar M.


Attachment: Selection_edafc0d7.zip


CH Clay Hess April 21, 2022 03:57 PM UTC

How would I implement in a UserControl and not a Window?



DM Dhanasekar Mohanraj Syncfusion Team April 22, 2022 03:17 PM UTC

Hi Clay Hess,

Currently, we are checking the feasibilities to achieve your requirement. We will check and update you further details on April 26, 2022.

We appreciate your patience until then.

Regards,

Dhanasekar M.



DM Dhanasekar Mohanraj Syncfusion Team April 26, 2022 01:58 PM UTC

Hi Clay Hess,

You can achieve your requirement by using below code snippet,

C#:

internal class SelectionBehavior : Behavior<SfDataGrid>

{

    protected override void OnAttached()

    {

        this.AssociatedObject.PreviewMouseUp += AssociatedObject_PreviewMouseUp;

    }

 

    private void AssociatedObject_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)

    {

        var rowIndex = this.AssociatedObject.GetVisualContainer().PointToCellRowColumnIndex(e.GetPosition(this.AssociatedObject.GetVisualContainer())).RowIndex;

        var record = this.AssociatedObject.GetRecordAtRowIndex(rowIndex);

        if (this.AssociatedObject.SelectedItems.Contains(record))

            this.AssociatedObject.SelectedItem = null;

    }

 

    protected override void OnDetaching()

    {

        this.AssociatedObject.PreviewMouseUp -= AssociatedObject_PreviewMouseUp;

    }

 

}


We had modified the sample based on your requirement. Please check this and revert us if you need further assistance for this.

Regards,
Dhanasekar M.


Attachment: Sample_d8319124.zip

Loader.
Up arrow icon