フィルタリングする必要のあるコレクションを表示するUserControlがありますが、コントロールに渡されたコレクションがメインウィンドウでフィルタリングされる場合があります。次のようになります。
Window1.xaml
<Window x:Class="CollectionViewSourceTesting.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CollectionViewSourceTesting="clr-namespace:CollectionViewSourceTesting"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<CollectionViewSource Source="{Binding Data}" x:Key="ItemsViewSource" Filter="CollectionViewSource_Filter" />
</Window.Resources>
<Grid>
<CollectionViewSourceTesting:UserControl1 DataContext="{Binding Source={StaticResource ItemsViewSource}}" />
</Grid>
</Window>
UserControl1.xaml
<UserControl x:Class="CollectionViewSourceTesting.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<Grid.Resources>
<CollectionViewSource Source="{Binding}" x:Key="cvs" Filter="CollectionViewSource_Filter" />
</Grid.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource cvs}}" />
</Grid>
</UserControl>
CollectionViewSource.SourceはListCollectionViewを受け入れないため、これは機能しません。「'System.Windows.Data.ListCollectionView'はプロパティ'Source'の有効な値ではありません」という例外がスローされます。
UserControlでDataContextChangedイベントを使用して、ListCollectionViewに手動でフィルタリングを追加できると考えましたが、それはネストされず、ListBox.ItemsSourceプロパティも手動で設定する必要があります。既存のビューを使用して、Filterプロパティを変更します。私がやりたいのは、CollectionViewSourceを使用して別のビューを作成し、xamlを挿入できるようにすることです。それは可能ですか?
UserControlでCollectionViewsだけでなく通常のコレクションも処理できるようにしたい。