0

I am here with a new problem.

I have a DataGrid and a TextBox. I would like to filter the DataGrid based on the TextBox's value.

I have done that using MarkupExtensions as mentioned here.

Now it works fine upto the Value property (Property of PropertyFilter class as mentioned in above link) is a string mentioned in XAML. When I change it to binding it stops working. Here is my XAML with binding:

<CollectionViewSource x:Key="GroupsViewSource" Source="{Binding Groups}">
    <CollectionViewSource.Filter>
        <me:Filter>
            <me:PropertyFilter PropertyName="GroupName"
                               Value="{Binding SearchGroupName}" />
        </me:Filter>
    </CollectionViewSource.Filter>
</CollectionViewSource>

SearchGroupName is a simple Property of type string in my ViewModel.

I have also tried to change the binding as follows:

Value = "{Binding DataContext.SearchGroupName, RelativeSource={RelativeSource 
                               Mode=FindAncestor, AncestorType={x:Type UserControl}}}"

I have tried to debug it using System.Diagnostics as follows:

<CollectionViewSource x:Key="GroupsViewSource" Source="{Binding Groups}">
    <CollectionViewSource.Filter>
        <me:Filter>
            <me:PropertyFilter PropertyName="GroupName"
                               Value="{Binding SearchGroupName, diag:PresentationTraceSources.TraceLevel=High}" />
        </me:Filter>
    </CollectionViewSource.Filter>
</CollectionViewSource>

But then I get a compilation error : unknown property PresentationTraceSources.TraceLevel for System.Windows.Data.Binding......

I think my binding with RelativeSource does not work because I think that CollectionViewSource is not a member of Visual/Logical Tree.

Thus I think my DataContext might be null. What are the solutions that you guys prefer if you are in the same situation?????

Community
  • 1
  • 1
Vishal
  • 6,238
  • 10
  • 82
  • 158

1 Answers1

1

You can try moving the filter into the resources of the framework element with the correct DataContext, using a binding like Path=DataContext.<property-name>, Source={x:Reference <element-name>}. Reference the filter where you need it using StaticResource.

This workaround is useful for binding in collection containers for example.


Code should be something like this (untested):

<SomeElement Name="el">
    <SomeElement.Resources>
        <me:PropertyFilter x:Key="Filter1" PropertyName="GroupName"
                           Value="{Binding DataContext.SearchGroupName, Source={x:Reference el}}" />
        <CollectionViewSource x:Key="GroupsViewSource" Source="{Binding Groups}">
            <CollectionViewSource.Filter>
                <me:Filter>
                    <StaticResource ResourceKey="Filter1"/>
                </me:Filter>
            </CollectionViewSource.Filter>
        </CollectionViewSource>
Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I have used `x:Reference` as you suggested. Now my Binding is correct as I put the ValueConverter and put a breakpoint in it, it shows expected value. But dataGrid is not filtered when I write anything in textbox. I have also changed the UpdateSourceTrigger to PropoertyChanged. But when I click on Column Header of DataGrid, the Filter works. – Vishal Nov 27 '14 at 06:39
  • There is no way for the `CollectionViewSource` to know that the filter event method's results changed, you need to update it somehow. Don't know how one would do that though... – H.B. Nov 27 '14 at 09:04
  • Thanks. Then I will have to find another approach for filtering data. – Vishal Nov 27 '14 at 09:34
  • @Vishal: Did you find a way? – SmallestWish Mar 12 '20 at 14:02
  • @stalbaig I have left WPF some years ago. So, I don't remember that. Sorry! my friend! – Vishal Mar 13 '20 at 18:27