dotnet / dotnet/wpf

DataGrid anchor selection does not use initial selection

Open
#10,780 1 comment 2 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

When using a `DataGrid` with Extended `SelectionMode` which has an initial selection, an anchor selection does not work as expected.

### Reproduction Steps

[Repro project with DataGrid and ListBox](https://github.com/user-attachments/files/19773685/AnchorSelectionRepro.zip)
E.g. in the example project.
- Item 1 is initially selected
- Shift-Click on Item 3

Additional Scenario:
- Now Select Item 1 via the `ListBox`
- Shift Click on Item 5 in the `DataGrid`

### Expected behavior

- Item 1, Item 2 and Item 3 are selected after Shift-Click.

This expected behavior is the behavior that can be observed in the `ListBox`.
In the `DataGrid` this behavior occurs only when the initial selection was done by user input.

Additional Scenario
- All items should be selected

### Actual behavior

Only Item 3 is selected after the Shift-Click

Additional Scenario:
Only Items 3 to 5 are selected

### Regression?

Tested in NetFx 4.81: same issue

### Known Workarounds

_No response_

### Impact

_No response_

### Configuration

_No response_

### Other information

`_selectionAnchor` is only updated on user input and the selection code does not check `_selectionAnchor` against the current true selection.
[DataGrid.cs](https://source.dot.net/#PresentationFramework/System/Windows/Controls/DataGrid.cs,ff3321e8c5945e53,references)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.