我在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>
答案 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}"/>