You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF中RadGridView集合列(Category)筛选无法显示去重值问题

Fixing Distinct Filter for Collection-Based RadGridView Column

Got it, let's break down why your current setup isn't working and fix it step by step. The core issue is that your code is trying to get distinct values for the entire CategoryList collection objects (each row's CategoryList is a separate instance), not the individual CategoryText values inside those collections. That's why the filter popup isn't showing the unique category texts you expect.

Step 1: Rewrite the DistinctValuesLoading Event Handler

Instead of relying on GetDistinctValues for the column (which targets the collection itself), we need to manually extract all CategoryText values from every row's CategoryList, then deduplicate them.

Replace your existing event code with this:

private void MyGrid_OnDistinctValuesLoading(object sender, GridViewDistinctValuesLoadingEventArgs e)
{
    // Only handle the Category column to avoid interfering with other columns
    if (e.Column.Name != "Category") return;

    var radGridView = sender as RadGridView;
    if (radGridView == null || radGridView.ItemsSource == null)
    {
        e.ItemsSource = Enumerable.Empty<string>();
        return;
    }

    // Replace these placeholders with your actual data types:
    // YourDataItemType = the class of items bound to your RadGridView (e.g., Product, Order)
    // YourCategoryItemType = the class of objects in CategoryList (with CategoryText property)
    var uniqueCategoryTexts = radGridView.ItemsSource.Cast<YourDataItemType>()
        // Flatten all nested CategoryList collections into a single list of category items
        .SelectMany(item => item.CategoryList ?? Enumerable.Empty<YourCategoryItemType>())
        // Extract the CategoryText property from each category item
        .Select(category => category.CategoryText)
        // Optional: Filter out empty/null texts for cleaner results
        .Where(text => !string.IsNullOrWhiteSpace(text))
        // Get only unique values
        .Distinct()
        // Sort values for better user experience
        .OrderBy(text => text)
        .ToList();

    e.ItemsSource = uniqueCategoryTexts;
}

Key Details:

  • The SelectMany method is critical here—it takes all the nested CategoryList collections and flattens them into one list of individual category items, allowing us to extract their text values.
  • Don't forget to replace the placeholder types (YourDataItemType, YourCategoryItemType) with your actual entity classes.

Step 2: Double-Check XAML Settings

Your existing XAML is mostly correct, but confirm these points to ensure consistency:

  • FilterMemberPath="CategoryText" is still set on the GridViewDataColumn (you already have this, which helps align with the filtering logic).
  • IsFilterable="True" remains enabled (you've already configured this correctly).

Why Your Original Code Failed

The GetDistinctValues(e.Column, false) method was returning distinct collection instances, not distinct text values. Since each row's CategoryList is a separate object (even if it contains the same category texts), the method treated each collection as a unique value. That's why your filter popup wasn't showing the unique category texts you wanted.

内容的提问来源于stack exchange,提问作者user3848036

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:56:18