XAML:x:静态资源数组

时间:2017-07-10 21:02:08

标签: wpf staticresource

我在BitmapImage

中声明了一些ResourceDictionary
<BitmapImage x:Key="LockImageSource"   UriSource="img/lock.png"   />
<BitmapImage x:Key="UnlockImageSource" UriSource="img/unlock.png" />
...

我的转换器使用两个ImageSource元素的数组参数来选择要显示的图像之一,具体取决于传递的值:

<Image Source="{Binding Path=IsLocked,
                   Converter={StaticResource AlteringConverter},
          ConverterParameter={StaticResource LockUnlockImageSourcePair}}"
       Width="16" Height="16" />

LockUnlockImageSourcePair资源应如何显示?

<x:Array x:Key="LockUnlockImageSourcePair" Type="{x:Type BitmapImage}">
    <??? />
    <??? />
</x:Array>

1 个答案:

答案 0 :(得分:2)

您可以将此简单样式与DataTrigger一起使用,而不是使用转换器和复杂的ConverterParameter进行绑定:

<Image Width="16" Height="16">
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Source" Value="{StaticResource UnlockImageSource}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsLocked}" Value="True">
                    <Setter Property="Source" Value="{StaticResource LockImageSource}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

如果要将样式重用于多个图像控件,可以将其声明为资源:

<Style x:Key="LockUnlockImageStyle" TargetType="Image">
    <Setter Property="Width" Value="16"/>
    <Setter Property="Height" Value="16"/>
    <Setter Property="Source" Value="{StaticResource UnlockImageSource}"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsLocked}" Value="True">
            <Setter Property="Source" Value="{StaticResource LockImageSource}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
...

<Image Style="{StaticResource LockUnlockImageStyle}"/>