如何绑定祖先和自我的相对来源?

时间:2014-02-17 17:45:02

标签: c# wpf binding datacontext relativesource

我有以下代码:

<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中的每个键(枚举)都有一个不同的真/假图像。 所以我试着写:

<Setter Property="Source" Value="{Binding Path=DataContext.SecDic[Key],
                      RelativeSource={AncestorType={x:ItemsControl}}}"/>

但它不起作用,因为“Key”属于图像(MyDictionary)的数据上下文,我将相对源更改为items控件,因此没有“Key”。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我要解决的问题是多绑定+自定义多重绑定转换器。 字典有一个绑定,密钥有一个绑定。