A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
Thank you for reaching out. In WPF DataGrid, when you sort by clicking on a column header, the control recreates the row containers based on the sorted ItemSource. If your columns are unbound, their values are not persisted, which is why they appear blank after sorting. This is expected behavior in WPF.
Recommended Approach
Best Practice: Convert Unbound Columns to Bound Columns
if the values are calculated from other columns or database values, expose them as properties in your data model.
- Add calculated properties to your ViewModel/ model class.
- Raise INotifyPropertyChanged when dependent values change.
- Bind the DataGrid columns directly to those properties. Example:
public string CalculatedValue { get => _calculatedValue; set { _calculatedValue = value; OnPropertyChanged(nameof(CalculatedValue)); } }
This ensures values remain intact during sorting, filtering and virtualization
Use IValueConverter or MultiBinding
if column value is derived from multiple fields:
<DataGridTextColumn Header="Total">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource TotalConverter}">
<Binding Path="Value1"/>
<Binding Path="Value2"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
This keeps calculations fully data-bound and sorting-safe.
Avoid setting value in Ui events
Do not rely on:
- LoadingRow
- RowLoaded
- Code-behind cell assignment
These will be lost whenever sorting, grouping or virtualization occurs.
Please let us know if you require any further assistance we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer".