在WPF中具有列排序的ListView

本文关键字:排序 ListView WPF | 更新日期: 2023-09-27 18:29:31

这实际上是一个通用的ListView,对于不同的数据,它将在应用程序的许多部分中可见(我不知道它将包含哪些数据以及它将有多少列),现在它工作得很好,但用户应该能够通过点击ListView控件的标题来对每一列进行排序,我不知道如何添加排序功能。

这是XAML代码:

<entityList:EntityList 
    Name="EntityList"
    BorderBrush="DarkSlateBlue"
    Visibility="{Binding IsResultsGridVisible, Converter={x:Static converters:BoolToVisibility.Instance}}"                                        
    ItemsSource="{Binding SearchEntities}"                           
    Style="{StaticResource EntityListStyle}"
    ItemsNumber="50"
    View="{Binding ColumnConfig, Converter={ x:Static converters:ConfigToGridViewConverter.Instance}}">
</entityList:EntityList>

这是C#代码:

internal class ConfigToGridViewConverter : IValueConverter
{
    public static readonly ConfigToGridViewConverter Instance = new ConfigToGridViewConverter();
    private ConfigToGridViewConverter()
    {
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var config = value as ColumnConfig;
        var gridView = new GridView { AllowsColumnReorder = true };
        if (config == null)
        {
            return gridView;
        }
        foreach (var colCfg in config.Columns)
        {
            var binding = new Binding(colCfg.DataField);
            switch (colCfg.Type)
            {
                case AttributeType.Enumerated:
                    if (colCfg.CodeListType != null)
                    {
                        binding.Converter = CodeListIdToDisplayNameConverter.Instance;
                        binding.ConverterParameter = colCfg.CodeListType.Value;
                    }
                    break;
                case AttributeType.Boolean:
                    binding.Converter = BooleanWithUndefinedToLocalisedStringConverter.Instance;
                    break;
                case AttributeType.Date:
                    binding.Converter = DateToShortLocalisedStringConverter.Instance;
                    break;
            }
            var col = new GridViewColumn
            {
                Header = colCfg.Header, // Column headers will need to be localised either when initializing config or at GridView creation time
                DisplayMemberBinding = binding,
            };
            gridView.Columns.Add(col);
        }
        return gridView;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

你知道我该怎么做吗?

在WPF中具有列排序的ListView

  1. 设置GridViewColumnHeader,如下所示:

    <GridViewColumn DisplayMemberBinding="{Binding Address}">
       <GridViewColumnHeader Content="Address" Click="GridViewColumnHeader_Click"/>
    </GridViewColumn>
    

    背后的代码

      private void GridViewColumnHeader_Click(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader colHeader = (GridViewColumnHeader)e.OriginalSource;
            string colName = colHeader.Content.ToString();
            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(LstView.ItemsSource);
            view.SortDescriptions.Add(new SortDescription(colName, ListSortDirection.Ascending));
            view.Refresh();
        }
    
  2. 指定一个HeaderTemplate,以便可以处理MouseDownEvent

然后你必须使用CollectionViewSource

 private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
 {
     string colName = ((TextBlock)sender).Text.ToString();
     CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(LstView.ItemsSource);
     view.SortDescriptions.Add(new SortDescription(colName, ListSortDirection.Ascending));
     view.Refresh();
 }

动态添加列:

            GridView view = (GridView)LstView.View;
            GridViewColumnHeader colHeader = new GridViewColumnHeader() { Content = "Name" };
            colHeader.Click += colHeader_Click;
            view.Columns.Add(new GridViewColumn()
            {
                DisplayMemberBinding = new Binding("Name"),
                Header = colHeader
            });