次のコードがあります。
<ItemsControl ItemsSource="{Binding MyDictionary}" >
<Image Width="16">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Value}" Value="False">
<Setter Property="Source" Value="{Binding RelativeSource =
{RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}},
Path=DataContext.SecDic}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Value}" Value="True">
<Setter Property="Source" Value="{Binding RelativeSource =
{RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}},
Path=DataContext.ThirdDic}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ItemsControl>
MyDictionary - キーが enum 型のディクショナリです。このバインドは、SecDic と ThirdDic をディクショナリから BitmapImages に変更すると機能します。しかし、MyDictionary のキー (enum) ごとに、異なる True/False イメージが存在するようにしたいと考えています。だから私は書いてみました:
<Setter Property="Source" Value="{Binding Path=DataContext.SecDic[Key],
RelativeSource={AncestorType={x:ItemsControl}}}"/>
しかし、「キー」は (MyDictionary の) 画像のデータ コンテキストに属しており、相対ソースをアイテム コントロールに変更したため、「キー」が存在しないため、機能しません。助言がありますか?