Hi,
I want to sort the column by click the header.
I follow the method below.
https://code.msdn.microsoft.com/windowsdesktop/Sorting-a-WPF-ListView-by-5769086f#content
but cannot success.
it throws exception in this line resultDataView.SortDescriptions.Clear() - System.NotSupportedException.
I check the attribute of resultDataView.
ICollectionView resultDataView = CollectionViewSource.GetDefaultView(lvSelectableOpenPosition.ItemsSource);
CanSort=false.
I don't know it is related or not.
Please help.
My program:
public partial class SelectableOpenPositionView : UserControl
{
private ListSortDirection _sortDirection;
private GridViewColumnHeader _sortColumn;
public SelectableOpenPositionView()
{
InitializeComponent();
}
private void SecondResultDataViewClick(object sender, RoutedEventArgs e)
{
GridViewColumnHeader column = e.OriginalSource as GridViewColumnHeader;
if (column == null)
{
return;
}
if (_sortColumn == column)
{
// Toggle sorting direction
_sortDirection = _sortDirection == ListSortDirection.Ascending ?
ListSortDirection.Descending :
ListSortDirection.Ascending;
}
else
{
// Remove arrow from previously sorted header
if (_sortColumn != null)
{
_sortColumn.Column.HeaderTemplate = null;
_sortColumn.Column.Width = _sortColumn.ActualWidth - 20;
}
_sortColumn = column;
_sortDirection = ListSortDirection.Ascending;
column.Column.Width = column.ActualWidth + 20;
}
if (_sortDirection == ListSortDirection.Ascending)
{
column.Column.HeaderTemplate =
Resources["HeaderTemplateArrowUp"] as DataTemplate;
}
else
{
column.Column.HeaderTemplate =
Resources["HeaderTemplateArrowDown"] as DataTemplate;
}
string header = string.Empty;
// if binding is used and property name doesn't match header content
Binding b = _sortColumn.Column.DisplayMemberBinding as Binding;
if (b != null)
{
header = b.Path.Path;
}
//lvSelectableOpenPosition.Items.SortDescriptions.Clear();
//lvSelectableOpenPosition.Items.SortDescriptions.Add(new SortDescription(header, _sortDirection));
ICollectionView resultDataView = CollectionViewSource.GetDefaultView(lvSelectableOpenPosition.ItemsSource);
resultDataView.SortDescriptions.Clear();
resultDataView.SortDescriptions.Add(new SortDescription(header, _sortDirection));
}
}
xaml
<UserControl x:Class="Administrator.View.OpenPositions.SelectableOpenPositionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
>
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate x:Key="HeaderTemplateArrowUp">
<DockPanel LastChildFill="True" Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumnHeader}}}">
<Path x:Name="arrowUp" StrokeThickness="1" Fill="Gray" Data="M 5,10 L 15,10 L 10,5 L 5,10" DockPanel.Dock="Right" Width="20"
HorizontalAlignment="Right" Margin="5,0,5,0" SnapsToDevicePixels="True"/>
<TextBlock Text="{Binding }" />
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="HeaderTemplateArrowDown">
<DockPanel LastChildFill="True" Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumnHeader}}}">
<Path x:Name="arrowDown" StrokeThickness="1" Fill="Gray" Data="M 5,5 L 10,10 L 15,5 L 5,5" DockPanel.Dock="Right" Width="20"
HorizontalAlignment="Right" Margin="5,0,5,0" SnapsToDevicePixels="True"/>
<TextBlock Text="{Binding }" />
</DockPanel>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<ListView x:Name="lvSelectableOpenPosition"
ItemsSource="{Binding RecordList}"
IsEnabled="{Binding IsEnabled}"
Grid.Row="1"
GridViewColumnHeader.Click="SecondResultDataViewClick">
<ListView.View>
<GridView>
<GridView.Columns>
<!--<GridViewColumn Header="{StaticResource Common_Ref}" DisplayMemberBinding="{Binding Path=Record.Ref}"/>-->
<GridViewColumn DisplayMemberBinding="{Binding Path=Record.Ref}">
<GridViewColumnHeader Content="{StaticResource Common_Ref}" />
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>