mgierw
mgierw

Reputation: 75

XAML - CollectionViewSource when passed to custom control stops filtering

I have the following CollectionViewSource in the main window and a custom control that utilizes that source:

<CollectionViewSource x:Key="cl1List" Source="{Binding Path=ResAmountMap}" Filter="OnCl1Filter"/>
//....
<control:ResAmounListView ResAmountMap="{Binding Source={StaticResource cl1List}}" />

The custom control is using ListView:

<ListView ItemsSource="{Binding Path=ResAmountMap}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Resource" DisplayMemberBinding="{Binding Path=Key}" />
        </GridView>
    </ListView.View>
</ListView>

The thing is that the ListView in custom control displays all elements from the source, but I expect it will display filtered ones. When I used ListView directly in the main window (without custom control) then it displayed filtered only items.

Any ideas why filtering stops working when CollectionViewSource is passed to custom control?

Upvotes: 0

Views: 186

Answers (1)

Clemens
Clemens

Reputation: 128061

Assuming that you have (correctly!) not explicitly set the control's DataContext property, the expression

ItemsSource="{Binding Path=ResAmountMap}"

binds directly to the inherited DataContext, and hence directly to the source property in the view model (which accidentially has the same name as the control property).

In other words, the ListView in your control does not use the control property at all.

Write the binding like shown below, and in case it is not a UserControl, replace UserControl by the actual base class of your control.

ItemsSource="{Binding Path=ResAmountMap,
              RelativeSource={RelativeSource AncestorType=UserControl}}"

Upvotes: 1

Related Questions