dstavila
Topic Author
Posts: 4
Joined: 08 Dec 2020, 15:31

CollectionFilterBehavior example

13 Jan 2023, 13:01

Are there any examples on how to use CollectionFilterBehavior with custom struct/type?
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: CollectionFilterBehavior example

13 Jan 2023, 20:22

This is a simple xaml using the CollectionFilterBehavior:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
  xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">
  <TextBox Text="{Binding SearchText}"/>
  <ListBox ItemsSource="{Binding FilteredItems, ElementName=FilterBehavior}">
    <b:Interaction.Behaviors>
      <noesis:CollectionFilterBehavior x:Name="FilterBehavior" ItemsSource="{Binding Books}" Predicate="{Binding TextPredicate}" />
    </b:Interaction.Behaviors>
  </ListBox>
</Grid>
For an example of custom predicate you can look at our StringFilterPredicate (C++ source code is also provided in our Native SDK).
 
dstavila
Topic Author
Posts: 4
Joined: 08 Dec 2020, 15:31

Re: CollectionFilterBehavior example

13 Jan 2023, 21:05

This is a simple xaml using the CollectionFilterBehavior:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
  xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">
  <TextBox Text="{Binding SearchText}"/>
  <ListBox ItemsSource="{Binding FilteredItems, ElementName=FilterBehavior}">
    <b:Interaction.Behaviors>
      <noesis:CollectionFilterBehavior x:Name="FilterBehavior" ItemsSource="{Binding Books}" Predicate="{Binding TextPredicate}" />
    </b:Interaction.Behaviors>
  </ListBox>
</Grid>
For an example of custom predicate you can look at our StringFilterPredicate (C++ source code is also provided in our Native SDK).
I saw documentation for it, but I'm not getting how exactly I can bind it from C# side of it. Can you show example of TextPredicate? I can extrapolate to custom one.
 
dstavila
Topic Author
Posts: 4
Joined: 08 Dec 2020, 15:31

Re: CollectionFilterBehavior example

16 Jan 2023, 15:22

Ok I've figured out how to use it, one question remained - how can I use it on HierarchicalDataTemplate to filter nested stuff?
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: CollectionFilterBehavior example

17 Jan 2023, 21:32

I'm not getting how exactly I can bind it from C# side of it. Can you show example of TextPredicate?
The ViewModel should expose a property with the filter predicate object:
public class MyViewModel
{
  public StringFilterPredicate TextPredicate { get; private set; }
  
  public string SearchText
  {
    get { return TextPredicate.Filter; }
    set { TextPredicate.Filter = value; }
  }
  
  public MyViewModel() { TextPredicate = new StringFilterPredicate(StringFilterMode.Contains); }
}
how can I use it on HierarchicalDataTemplate to filter nested stuff?
Are you going to use it in a TreeView?
 
dstavila
Topic Author
Posts: 4
Joined: 08 Dec 2020, 15:31

Re: CollectionFilterBehavior example

18 Jan 2023, 14:20

I'm not getting how exactly I can bind it from C# side of it. Can you show example of TextPredicate?
The ViewModel should expose a property with the filter predicate object:
public class MyViewModel
{
  public StringFilterPredicate TextPredicate { get; private set; }
  
  public string SearchText
  {
    get { return TextPredicate.Filter; }
    set { TextPredicate.Filter = value; }
  }
  
  public MyViewModel() { TextPredicate = new StringFilterPredicate(StringFilterMode.Contains); }
}
how can I use it on HierarchicalDataTemplate to filter nested stuff?
Are you going to use it in a TreeView?
Ideally - yeah
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: CollectionFilterBehavior example

30 Jan 2023, 11:20

Hi, sorry for the late reply.

To use it in a TreeView each item should have a filter behavior pointing to the source collection, you can do that in the style like this:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
  xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">
  <TreeView>
    <TreeView.ItemContainerStyle>
      <Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
        <Setter Property="noesis:StyleInteraction.Behaviors">
          <Setter.Value>
            <noesis:StyleBehaviorCollection>
              <noesis:CollectionFilterBehavior ItemsSource="{Binding Children}" Predicate="{Binding TextPredicate}"/>
            </noesis:StyleBehaviorCollection>
          </Setter.Value>
        </Setter>
      </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.ItemTemplate>
      <HierarchicalDataTemplate ItemsSource="{Binding (b:Interaction.Behaviors)[0].FilteredItems, RelativeSource={RelativeSource Self}}">
        <TextBlock Text="{Binding Name}"/>
      </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
  </TreeView>
</Grid>

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 14 guests