Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
asmirnov82 opened this issue Jan 23, 2025 · 0 comments

Comments

@asmirnov82
Copy link

asmirnov82 commented Jan 23, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant