CollectionFilterBehavior example
Are there any examples on how to use CollectionFilterBehavior with custom struct/type?
-
-
sfernandez
Site Admin
- Posts: 2727
- Joined:
Re: CollectionFilterBehavior example
This is a simple xaml using the CollectionFilterBehavior:
For an example of custom predicate you can look at our StringFilterPredicate (C++ source code is also provided in our Native SDK).
Code: Select all
<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>
Re: CollectionFilterBehavior example
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.This is a simple xaml using the CollectionFilterBehavior:For an example of custom predicate you can look at our StringFilterPredicate (C++ source code is also provided in our Native SDK).Code: Select all<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>
Re: CollectionFilterBehavior example
Ok I've figured out how to use it, one question remained - how can I use it on HierarchicalDataTemplate to filter nested stuff?
-
-
sfernandez
Site Admin
- Posts: 2727
- Joined:
Re: CollectionFilterBehavior example
The ViewModel should expose a property with the filter predicate object:I'm not getting how exactly I can bind it from C# side of it. Can you show example of TextPredicate?
Code: Select all
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); }
}
Are you going to use it in a TreeView?how can I use it on HierarchicalDataTemplate to filter nested stuff?
Re: CollectionFilterBehavior example
Ideally - yeahThe ViewModel should expose a property with the filter predicate object:I'm not getting how exactly I can bind it from C# side of it. Can you show example of TextPredicate?Code: Select allpublic 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); } }
Are you going to use it in a TreeView?how can I use it on HierarchicalDataTemplate to filter nested stuff?
-
-
sfernandez
Site Admin
- Posts: 2727
- Joined:
Re: CollectionFilterBehavior example
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:
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:
Code: Select all
<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: Satellite5 and 0 guests