views:

279

answers:

3

Hi

I want to have an image bound to a boolean and have the source of the image to depend on the boolean value

i.e. true source="image1" false source="image2"

I was wondering if there is a way to do it inline without need for a converter.

A: 

If you're just binding the Image::Source property directly then the only way to accomplish this is with a custom IValueConverter.

Drew Marsh
+6  A: 

You can create a style on the Image which uses a DataTrigger to swap the image source depending on a binding. In this example the image changes depending on the value of a boolean called simply "Value".

    <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>
Groky
A: 

Fantastic solution. Is there a way to make this a resource and "pass-in" (I understand that this is not the correct terminology in this case) the bool property? It would be very helpful for promoting XAML reuseablity.

RockyMountainHigh