MVVM Binding TreeListView Filters

Grids for WPF Forum

Posted 8 months ago by Molly
Version: 22.1.3

I was wondering if there is an example project anywhere outlining how to bind TreeListView.DataFilter to an object in the ViewModel. The end use case being that the user can add or remove filters at runtime through a custom User Control, so one / many / no filters can be applied.

In my view I am expecting to do something like:

 <grids:TreeListView x:Name="ProjectTreeListView" 
                     RootItem="{Binding ProjectTreeRoot}" 
                     IsFilterActive="{Binding ProjectFilterActive}" 
                     DataFilter="{Binding ProjectFilter}">...

Then in the ViewModel have my dependency properties:

  private IDataFilter _projectFilter;

        public IDataFilter ProjectFilter
            get => _projectFilter;
                SetProperty(ref _projectFilter, value);

        private bool _projectFilterActive;

        public bool ProjectFilterActive
            get => _projectFilterActive;
                SetProperty(ref _projectFilterActive, value);

Or even binding to a Collection of IDataFilters, then in the ViewModel I should be able to add filters and activate / deactivate the filter like so:

ProjectFilter = new PredicateFilter(i => ((TreeNodeModel)i).Name.StartsWith("S"));
ProjectFilterActive = true; 

When I implement the code above I get a null reference error when the filter is actually called (unfortunately the error is not very clear, I think it's the tree items it isn't getting? ).

Maybe I have missed something simple, maybe I am approaching this entirely incorrectly, please let me know!

Comments (3)

Posted 8 months ago by Actipro Software Support - Cleveland, OH, USA

Hi Molly,

We don't have any samples other than those in the main sample project.  In theory what you are trying to do should work fine.

I did a quick test in the codebehind of our TreeListBoxFiltering QuickStart by adding this method:

protected override void OnMouseDoubleClick(MouseButtonEventArgs e) {

	treeListBox.DataFilter = new Windows.Data.Filtering.PredicateFilter(i => ((Common.TreeNodeModel)i).Name.StartsWith("S"));
	treeListBox.IsFilterActive = true;

When I double-clicked the area around the TreeListBox, I did see the filter get applied and only nodes with an "S" in them showed up.

Note that if you do have some kind of meta-filter where you have a number of them wrapped in wrapper filter, you can raise the filter's FilterChanged event when one of them is added/removed to let the control know to requery things.

Actipro Software Support

Posted 7 months ago by Molly

Hi, thanks for the response!

In this case I think I am maybe initialising my tree incorrectly, here is my theory:

When I add this predicate filter:

treeListBox.DataFilter = new Windows.Data.Filtering.PredicateFilter(i => ((Common.TreeNodeModel)i).Name.StartsWith("S"));

and apply it, I get a null value error. At the point of error, the value of i is the RootNode of my tree which has 3 children, it is these children that I want to check the names of. I think it is first checking the name on this root node, but my root node is empty of any values / properties except the children I have added. Thus it has no name to be checked and the null error is thrown.

Does this seem like it would be the cause to you? If so how can I fix it, if not I will keep trying some more changes!



Answer - Posted 7 months ago by Actipro Software Support - Cleveland, OH, USA


I think what's going on here is as you suspected, the root node is being passed to the filter.  You could work around that by adding text to your root node or checking for a null Name.

Keep in mind that some usage scenarios might have TreeListBox.IsRootItemVisible set to true.  We also discussed here possibly skipping filtering on the root node, but looking at the VS Solution Explorer, they hide everything (including the root node) if there are no matches.  Therefore it's probably best to keep the existing design in place.

Actipro Software Support

The latest build of this product (v23.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.