You can get it also using Behaviors. Because it is better to use behaviors in such situations.
This is what you are going to do:
First of all add SortOnlyAscending.cs class to your Project.
public class SortOnlyAscending:Behavior<DataGrid>
{
protected override void OnAttached()
{
AssociatedObject.Sorting += AssociatedObject_Sorting;
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.Sorting -= AssociatedObject_Sorting;
base.OnDetaching();
}
private void AssociatedObject_Sorting(object sender, DataGridSortingEventArgs e)
{
e.Column.SortDirection = ListSortDirection.Ascending;
}
}
Then in .xaml you going to add this behavior to your DataGrid like that:
<DataGrid>
<i:Interaction.Behaviors>
<local:SortOnlyAscending/>
</i:Interaction.Behaviors>
</DataGrid>
Also you have to add two namepsaces to your .xaml also for using your behavior. The name of my project was WpfApplication1, so you gonna chnage it as you want.
xmlns:local ="clr-namespace:WpfApplication1"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
That's it. Also you need System.Windows.interactivity.dll for using Behavior class.
You can download it from NUget Package Manager also. Here is link.