Skip to content

Fix DataGrid row anchor not being set when programmatically updating selection #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2021

Conversation

Metritutus
Copy link
Contributor

@Metritutus Metritutus commented Aug 8, 2021

When a change to the selection of WPF's DataGrid is made programmatically it does not update the selection anchor, which is used for SHIFT selection. As a result, attempting SHIFT selection following a programmatic adjustment to the selection of a DataGrid can result in unexpected selection behaviour for the end user.

You can test this behaviour in the Showcase application by:

  1. Select a row (eg row 3).
  2. Hold SHIFT and click on a row a few rows down from the first selected row (eg row 9).
  3. Let go of SHIFT and then click on a row in the middle of the selection (eg row 5).
  4. Hold SHIFT and click on a row a few rows down (eg row 12) from the currently selected row.

Without the fix in this pull request, the selection will select all the rows from row 3 to row 12, instead of row 5 to row 12.

What changed?

  • Updated ItemsControlExtensions to set the DataGrid's selection anchor when the selection is programmatically changed.

This change uses reflection to reference a private field in the DataGrid called _selectionAnchor. In looking over the source code for WPF, I cannot see an alternative way of doing this using a public (or even protected) API, although it's possible there is a way. Although this field is private (and therefore could theoretically change at any time), this WPF code has been in place for many years and so appears stable as far as future changes go.

In my own testing, this results in the selection anchor being set as expected and it all seems to behave fine, however I welcome input on this approach.

* Updated ItemsControlExtensions to set the DataGrid's selection anchor when the selection is programmatically changed.
@Metritutus Metritutus changed the title Update ItemsControlExtensions.cs Fix DataGrid row anchor not being set when programmatically updating selection Aug 8, 2021
* Updated anchor field functionality in SetItemSelected() in ItemsControlExtensions to properly account for situations where we may not want to update the selection anchor if it's valid and there are selected items but no selected cells.
@punker76 punker76 added this to the 2.4.0 milestone Aug 10, 2021
@punker76 punker76 merged commit 8cb6c08 into punker76:develop Aug 10, 2021
@punker76
Copy link
Owner

@Metritutus your changes have been merged, thanks for your contribution 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants