1

I got a row like this:

enter image description here

XAML:

<ListView x:Name="ListViewAnlagen"
          Grid.RowSpan="2"
          ItemContainerStyle="{StaticResource TempContainerStyle}"
          VerticalAlignment="Top" HorizontalAlignment="Left"
          Height="571" Width="1314"
          Margin="0,53,0,0"
          AlternationCount="2"
          GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
    <ListView.View>
        <GridView ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">
            <GridView.Columns>
                <GridViewColumn Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Border x:Name="border"
                                    BorderBrush="Gray" BorderThickness=".5" Margin="-6,-3">
                                <TextBlock Text="{Binding EqNr}" Margin="6,3"/>
                            </Border>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="EQ Nr."/>
                        <Image Source="img/filter.png"
                               Width="20" Height="20" Margin="25 0 0 0"
                               MouseDown="Image_MouseDown_1" />
                    </StackPanel>
                </GridViewColumn>

I have added a click handler: GridViewColumnHeader.Click

My Question is, how to sort this ascending and descending. I already looked at some others solutions, but it seems they only work when you bind it with DisplayMemberBinding.

What I already tried: this

Community
  • 1
  • 1
Jul Pod
  • 370
  • 3
  • 16
  • use `CollectionViewSource` and `SortDescription` – Lei Yang May 05 '17 at 06:39
  • Hello Sir, sorry i dont get it, do you got any example or so? – Jul Pod May 05 '17 at 06:56
  • Try this https://code.msdn.microsoft.com/windowsdesktop/Sorting-a-WPF-ListView-by-209a7d45 – Amol Bavannavar May 05 '17 at 06:56
  • [msdn](https://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) and [so](http://stackoverflow.com/questions/14992072/how-do-you-sort-a-collectionviewsource-by-one-property-then-by-another-as-a-tie) – Lei Yang May 05 '17 at 06:57
  • If you mean [link] (http://stackoverflow.com/questions/30787068/wpf-listview-sorting-on-column-click) its not working – Jul Pod May 05 '17 at 06:57
  • @AmolBavannavar this solution cannot working for me, i dont even got the GriedViewColumnHeader Option in my code, because i designed the columns individual, – Jul Pod May 05 '17 at 07:02

1 Answers1

2

Since you already examined the example as commented by @AmolBavannavar (https://code.msdn.microsoft.com/windowsdesktop/Sorting-a-WPF-ListView-by-209a7d45), here is a hybris between the example and your current approach.

The main obstacle in adapting the example is the usage of GridViewColumnHeader.Command and GridViewColumnHeader.CommandParameter. Your equivalent for the command is the GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler", but you still need an equivalent to the command parameter.

I suggest you create an attached string property for this purpose and use it to attach the sort property name to the GridViewColumn. For the sake of demonstration, I don't create a new property but instead misuse the TextSearch.TextPath attached property:

<GridViewColumn Width="100" TextSearch.TextPath="EqNr">

Note that the "EqNr" is the same as the property name that is used for binding inside the cell template later.

Now, everything is in place to be used inside the click handler.

  • Get the clicked column header
  • Get the associated column
  • Get the attached property value that contains the sort property name
  • Get the collection view that is associated with the items source (or items)
  • Change the sort descriptions of the collection view

Code with simplified sorting logic:

private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
{
    var h = e.OriginalSource as GridViewColumnHeader;
    if (h != null)
    {
        var propertyName = h.Column.GetValue(TextSearch.TextPathProperty) as string;
        var cvs = ListViewAnlagen.ItemsSource as ICollectionView ??
            CollectionViewSource.GetDefaultView(ListViewAnlagen.ItemsSource) ??
            ListViewAnlagen.Items;
        if (cvs != null)
        {
            cvs.SortDescriptions.Clear();
            cvs.SortDescriptions.Add(new SortDescription(propertyName, ListSortDirection.Descending));
        }
    }
}

Note that for the sake of demonstration I only clear the sort descriptions and add a static descending sort description. For your actual application, you may want to keep track (or analyze) the current sorting status for the column and then alternate between ascending and descending sort.

grek40
  • 13,113
  • 1
  • 24
  • 50
  • @JulPod thats interesting... so you have static items or what? For most practical MVVM cases you would have an `ItemsSource` – grek40 May 08 '17 at 07:50
  • Ye, i add the items like this: `items.add()`, i think i have to rethink my binding code, seems a lot easier to filter and so on later.. – Jul Pod May 08 '17 at 07:52
  • @JulPod If you don't use `ItemsSource`, you can also try to use `ListViewAnlagen.Items.SortDescriptions` in the place where I have `cvs.SortDescriptions` in my answer. Might work (can't test right now) – grek40 May 08 '17 at 07:53
  • just tested, not working, the listview just gets empty, but dont care, i just bind them with `xmldataprovider` , now its working, so thanks altough – Jul Pod May 08 '17 at 07:58
  • @JulPod by the way: I just tested the `ListViewAnlagen.Items.SortDescriptions` approach and it worked for me - no empty items. So maybe something else is interferring in your code. – grek40 May 08 '17 at 08:06
  • OMG how much should i say you sorry, i made a mistake, it actually works, thanks a lot man, do i may ask you about a method to filter, just if you know some fast – Jul Pod May 08 '17 at 08:11
  • @JulPod no need to apologize. Short: `cvs.Filter = SomeFilterMethod` with signature `bool SomeFilterMethod(object item)`. For details, better ask separate question. – grek40 May 08 '17 at 08:17