ContentControl
を使用してスタイル設定された を持つウィンドウがありContentTemplate
ます。
には、内にネストさContentTemplate
れた単純な が含まれています。グリッドには、コードを使用して設定されたプロパティがあります (a にバインドされています。これを cvs1 と呼びましょう)。ListBoxは Grid から継承し、ListBox 項目の人口は正常に機能しています。例えばListBox
Grid
DataContext
CollectionViewSource
ItemsSource
<Grid x:Name="Grid1">
<ListBox x:Name="ListBox1" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"/>
</Grid>
ListBox はスタイル設定されており、メイン スタイルは ResourceDictionary に格納されています。
を使用して ListBox のスタイル値を設定していますが、を使用して異なるセッターを動的に適用するItemTemplate
ことも必要です。DataTrigger
私が直面している課題は、DataTrigger 内で個別の CollectionViewSource (cvs2 と呼びましょう) へのバインディングを確立できないように見えることです。
<Style TargetType="{x:Type ListBox}">
<Style.Triggers>
<!-- This seems to be trying to bind to cvs1, the error is it can't find the property -->
<DataTrigger Binding="{Binding cvs2, Path=TemplateName}" Value="ABC">
<Setter Property="ItemTemplate">
</DataTrigger>
<!-- This just doesn't seem to work -->
<DataTrigger Binding="{Binding Source={StaticResource cvs2},Path=TemplateName}" Value="XYZ">
<Setter Property="ItemTemplate">
</DataTrigger>
</Style.Triggers>
</Style>
cvs1 と cvs2 の両方が a で定義されていResourceDictionary
ます。
<CollectionViewSource x:Key="cvs1" />
<CollectionViewSource x:Key="cvs2" />
そして、次のように参照されます。
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DataSources.xaml" />
</ResourceDictionary.MergedDictionaries>
私が直面しているように見える問題はItemTemplate
、ListBox から DataContext を継承していることであり、これを回避して cvs2 データ ソースへのバインディングを確立することができないようです。StaticResource
これはかなり日常的なバインディング タスクになると思いました。そうではないようです。
データをデバッグするために、グリッドの外側 (メイン ウィンドウ内) のラベルで次のコードをテストしました。
<Label Content="{Binding cvs2, Path=/TemplateName}"/>
これは機能し、Label には TemplateName の値が取り込まれます。
ただし、これをDataTrigger
内で使用するItemsTemplate
と、バインディングが確立されません。
内から cvs2 へのバインドを確立するにはどうすればよいItemTemplate
ですか?