我正在尝试对datagrid中的数据进行排序,但是当我点击与转换器绑定的列的标题时,没有任何反应.我使用MVVM模式.示例如下.在该示例中,网格显示列(类型),其显示人的类型,因此我使用转换器(类TypeValueConverter).当我使用此转换器时,网格不会对列类型进行排序.
<Window x:Class="GridSort.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:GridSort"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False">
<DataGrid.Resources>
<my:TypeValueConverter x:Key="typeConverter" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName" />
<DataGridTextColumn Binding="{Binding Surname}" Header="Surname" />
<DataGridTextColumn Binding="{Binding Converter={StaticResource ResourceKey=typeConverter}}" Header="Type" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
public class ViewModel
{
private ICollection<Person> people;
public ICollection<Person> People
{
get
{
if (this.people == null)
{
this.people = new List<Person>();
this.people.Add(new Student() { FirstName = "Charles", Surname = "Simons" });
this.people.Add(new Student() { FirstName = "Jake", Surname = …Run Code Online (Sandbox Code Playgroud)