I tried to sort my listview like this: http://ift.tt/18Sdmq4
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?
Aucun commentaire:
Enregistrer un commentaire