In a WPF DataGrid control, if you set a column for one of the default columns (for example, DataGridTextColumn or DataGridCheckBoxColumn), sort by that column and then change its value, the grid will be automatically re-sorted.
However, if you use a DataGridTemplateColumn (and enable column sorting), you can sort it, but changing the cell value in this column does not cause the grid to not be re-sorted. How can I persuade him to automatically restart re-sorting?
XAML:
<DataGrid Name="grid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="First name" Binding="{Binding First}"/>
<DataGridTemplateColumn Header="Last name" SortMemberPath="Last">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Last}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Binding:
ObservableCollection items = new ObservableCollection();
grid.ItemsSource = items;
items.Add(new Character() { First = "Homer", Last = "Simpson" });
items.Add(new Character() { First = "Kent", Last = "Brockman" });
items.Add(new Character() { First = "Montgomery", Last = "Burns" });
Here is my product class, just in case, which is relevant:
public class Character : INotifyPropertyChanged {
private string first, last;
public event PropertyChangedEventHandler PropertyChanged;
private void Notify(string name) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public string First { get { return first; } set { first = value; Notify("First"); } }
public string Last { get { return last; } set { last = value; Notify("Last"); } }
}