In my previous post I talked about how one can embed code in XAML using the Dynamic Expression API. I was thinking on how to expand this even more. And I figured out… What if I use this for the Filter event of the CollectionViewSource…. Well I tried it out and it ended up working quite well…. What is even better is that you can allow the user to enter a string that is then compiled in a Lambda expression and applied for filtering data😀
So basically my implementation includes an Attached Property that you can use for a CollectionViewSource. This property is called FilterExpression and is in the class called LambdaCollectionView. All you have to do is to pass a string that is the filter expression that you want to evaluate for filtering. As context for your expression there is a predefined variable called item that is basically the type of object that you have in the collection…. example, if you a list of strings you can treat item as a string (i.e you can use all string functions such as StartsWith etc….)
Let’s do a small example on how you can use this. Imagine you have a list of strings and you want to apply a filter that filters all strings that do not start with the character M. you would do something like this
As you can see here you have to use the $ symbol for strings just like in the LambdaValueConverter. Ok ok… so this is cool but what is even cooler is the fact that you can make the user decide how to filter🙂
In the demo app I created a textbox and a button. The user can enter an expression in the textbox and when the button is clicked I reset the FilterExpression property and bamm the filter changes… In the textbox the user does not need to use the $ symbol for strings since the textbox handles ” unlike the XAML parser….
This is very handy if you want to apply some basic filtering from the UI…. The filter also supports user friendly expressions like Not instead of != and many others.. For a complete list of supported expressions read the Dynamic Expressions.html under the Dynamic Query folder (inside the download)
Hope you like this and give me more feedback on the AvalonLambdas ….