1

I tried to sort my listview like this: http://www.wpf-tutorial.com/listview-control/listview-how-to-column-sorting/

XAML:

<ListView x:Name="lvComputers" HorizontalAlignment="Left" Height="440" Margin="10,43,0,0" VerticalAlignment="Top" Width="560" SelectionChanged="lvComputers_SelectionChanged">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="120" DisplayMemberBinding="{Binding computerName}">
                <GridViewColumn.Header>
                    <GridViewColumnHeader Tag="Computername" Click="lvComputersColumnHeader_Click">Computername</GridViewColumnHeader>
                </GridViewColumn.Header>
            </GridViewColumn>
            <GridViewColumn Width="80" DisplayMemberBinding="{Binding operatingSystem}">
                <GridViewColumn.Header>
                    <GridViewColumnHeader Tag="Betriebssystem" Click="lvComputersColumnHeader_Click">Betriebssystem</GridViewColumnHeader>
                </GridViewColumn.Header>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

C#:

private void loadComputers()
{
    lvComputers.ItemsSource = mainController.getComputers();
    lvComputers.Items.SortDescriptions.Add(new SortDescription("Computername", ListSortDirection.Ascending));
    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvComputers.ItemsSource);
    view.SortDescriptions.Add(new SortDescription("Computername", ListSortDirection.Ascending));
    view.Filter = UserFilter;

}

private void lvComputersColumnHeader_Click(object sender, RoutedEventArgs e)
{
    GridViewColumnHeader column = (sender as GridViewColumnHeader);
    string sortBy = column.Tag.ToString();
    if (listViewSortCol != null)
    {
        AdornerLayer.GetAdornerLayer(listViewSortCol).Remove(listViewSortAdorner);
        lvComputers.Items.SortDescriptions.Clear();
    }

    ListSortDirection newDir = ListSortDirection.Ascending;
    if (listViewSortCol == column && listViewSortAdorner.Direction == newDir)
        newDir = ListSortDirection.Descending;

    listViewSortCol = column;
    listViewSortAdorner = new SortAdorner(listViewSortCol, newDir);
    AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
    lvComputers.Items.SortDescriptions.Add(new SortDescription(sortBy, newDir));
}

"getComputers" returns a list of computers based on this class:

public class Computers
{
    public String computerName { get; set; }
    public String operatingSystem { get; set; }
}

UserFilter is for a searchbox. But for some reason the sorting wont work.

I tried to add this

lvComputers.Items.SortDescriptions.Add(new SortDescription("Computername", ListSortDirection.Ascending));

to

loadComputers();

but it doesnt work either.

what am i doing wrong?

1 Answer 1

4

SortDescription strings are case sensitive:

 new SortDescription("Computername", ...

should be

 new SortDescription("computerName"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.