dotnet / dotnet/wpf

Datagrid column sort is not reacting to databinding changes

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

Description

### Description

Has this been always the case? When I sort a column in a Datagrid by clicking on it, it only sorts the column once. If anything in the cells of the column changes, the column is not sorted again. I tried with and without CollectionView (but I didn't used the collectionview sort because if the Datagrid already has sorting I expect it to work on its own)

Shouldn't the column sort react to databinding changes? What's the purpose of allowing sort in WPF when, if it has to react on databinding changes, we have to add our own sorting instead?

### Reproduction Steps

1. Use code below
2. Launch app
3. Click on the column to sort
4. Press the button

```XAML












```

```C#
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = this;
InitializeComponent();
Items.Add(new MyData { Name = "Item 1" });
Items.Add(new MyData { Name = "Item 2" });
Items.Add(new MyData { Name = "Item 3" });
Items.Add(new MyData { Name = "Item 4" });
Items.Add(new MyData { Name = "Item 5" });
}

public ObservableCollection Items { get; } = new();

private void Button_Click(object sender, RoutedEventArgs e)
{
Items[2].Name = "Changed";
}
}
public class MyData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

string _Name;
public string Name
{
get => _Name;
set
{
if (value == _Name) return;
_Name = value;
OnPropertyChanged(nameof(Name));
}
}
}
```

### Expected behavior

![Image](https://github.com/user-attachments/assets/62fb0335-e180-4fdb-8d06-7c8cfddf1bc9)

### Actual behavior

![Image](https://github.com/user-attachments/assets/0cc16e20-3fb4-425e-be38-27bb5937370a)

### Regression?

_No response_

### Known Workarounds

_No response_

### Impact

_No response_

### Configuration

.NET 8.0
x64

### Other information

_No response_

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.