Page 1 of 1

CollectionFilterBehavior example

Posted: 13 Jan 2023, 13:01
by dstavila
Are there any examples on how to use CollectionFilterBehavior with custom struct/type?

Re: CollectionFilterBehavior example

Posted: 13 Jan 2023, 20:22
by sfernandez
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).

Re: CollectionFilterBehavior example

Posted: 13 Jan 2023, 21:05
by dstavila
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.

Re: CollectionFilterBehavior example

Posted: 16 Jan 2023, 15:22
by dstavila
Ok I've figured out how to use it, one question remained - how can I use it on HierarchicalDataTemplate to filter nested stuff?

Re: CollectionFilterBehavior example

Posted: 17 Jan 2023, 21:32
by sfernandez
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?

Re: CollectionFilterBehavior example

Posted: 18 Jan 2023, 14:20
by dstavila
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

Re: CollectionFilterBehavior example

Posted: 30 Jan 2023, 11:20
by sfernandez
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>