ListView for WPF
ソート
ListView の使用 > ソート

When sorting is applied to a list view, the data is sorted in ascending or descending order.

The IDataCollection interface supports ascending and descending sorting for data controls, such as grids. It implements the SortAsync method to allow you to call the sorting operation in the collection view without having to cast to the specific interface. The SortAsync method sorts the collection view according to the specified sort parameters, descriptions, or path and direction.

The following code implements sorting in the ListView control using the SortAsync method.

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <c1:C1Button x:Name="btnSort" Margin="10 0 0 10" HorizontalAlignment="Left" Click="btnSort_Click" />
    <c1:C1ListView x:Name="listView" DisplayMemberPath="Name" Grid.Row="1" ItemHeight="30" />

</Grid>
SortDirection? sortDirection;
public MainWindow()
{
    InitializeComponent();
    // データソースに連結します。
    listView.ItemsSource = Person.Generate(100);
    UpdateButtonText();
}
// SortAsync メソッドでグループ化を追加します。
private async void btnSort_Click(object sender, RoutedEventArgs e)
{
    sortDirection = sortDirection != SortDirection.Ascending ? SortDirection.Ascending : SortDirection.Descending;
    UpdateButtonText();
    listView.ScrollToVerticalOffset(0);
    await listView.DataCollection.SortAsync("Name", sortDirection.Value);
}

private void UpdateButtonText()
{
    btnSort.Content = sortDirection != SortDirection.Ascending ? "Sort ascendingly" : "Sort descendingly";
}