Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

[Question] What is the best way to implement filtering of child elements in TreeDataGrid? #336

Open

Description

@asmirnov82

Hi.

I am using TreeDataGrid to show 2 level hierarchy and I would like to implement filtering: show children that starts with user defined substring('SearchText'). Filtering should happen dynamically (while user enters search text - the amount of shown items should decrease to show only child elements that satisfy already typed text). What is the best way to implement this functionality?

I came up to a solution (not working) with defining filtering inside childSelector of HierarchicalExpanderColumn:

ItemSource = new HierarchicalTreeDataGridSource<TreeItemNode>(Items)
{
    Columns =
    {
        new HierarchicalExpanderColumn<TreeItemNode>(new TextColumn<TreeItemNode, string>("Items", x => x.Name, width: new GridLength(1, GridUnitType.Star)),
        x => x.Children?.Where(child => string.IsNullOrEmpty(SearchText) || child.Name.StartsWith(SearchText)))
    }
};

...
internal class TreeItemNode : ObservableObject
{
    public string Name { get; init; }
    
    public IReadOnlyCollection<TreeItemNode>? Children { get; init; }

    public TreeItemNode(string name, TreeItemNode[]? children = null)
    {
        Name = name;
        Children = children;
    }
}

However, this requires all nodes to be collapsed/expanded for recalculation, that doesn't suite my needs. Could you please suggest a better approach or a way to force TreeDataGrid to refresh it's content?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions