温馨提示:本文翻译自stackoverflow.com,查看原文请点击:wpf - Bind Image.Source according to Boolean without a converter?
converters wpf binding conditional boolean-expression

wpf - 没有转换器就根据Boolean绑定Image.Source吗?

发布于 2020-04-05 20:42:19

我想有一个绑定到布尔值的图像,并且图像的来源取决于布尔值

即true source =“ image1” false source =“ image2”

我想知道是否有一种无需转换器就可以内联的方法。

查看更多

提问者
Shimmy
被浏览
67
Grokys 2009-11-03 23:33

您可以在图像上创建样式,该样式使用DataTrigger来根据绑定交换图像源。在此示例中,图像根据布尔值(简称为“值”)而变化。

    <Image Width="16">
        <Image.Style>
            <Style TargetType="{x:Type Image}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Value}" Value="False">
                        <Setter Property="Source" Value="Resources/image1.png"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Value}" Value="True">
                        <Setter Property="Source" Value="Resources/image2.png"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>