gpt4 book ai didi

c# - WPF中具有列排序的ListView

转载 作者:行者123 更新时间:2023-12-03 10:44:18 25 4
gpt4 key购买 nike

这实际上是一个通用的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();
}
}

知道我该怎么做吗?

最佳答案

  • 设置一个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();
    }
  • 指定一个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
    });

    关于c# - WPF中具有列排序的ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34689056/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com