wpf datatrigger on an image source
Asked Answered
A

1

17

Assuming the binding is right and the image files are where they shuld be, can anyone spot why the image in the xaml below won't change when the trigger evaluates to true?

Cheers,
Berryl

<Image Source="..\..\Images\OK.png" Grid.Column="2" Stretch="None">
    <Image.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding TimeSheet, Converter={StaticResource overQuotaConv}}" Value="True">
                    <Setter Property="Image.Source" Value="..\..\Images\Error.png"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>
Amorphism answered 10/3, 2010 at 3:39 Comment(0)
A
48

Try the following...

  • Set the TargetType="{x:Type Image}" on the Style
  • Change the setter's Property to Property="Source"

The way your property path is currently defined, it's looking for a property on Image called Image (which doesn't exist) then within that something called Source.

Additionally, remove the Source attribute from the Image tag at the top. This will override whatever is applied by the style. You can move it to another DataTrigger or you can put it in a normal setter like so:

<Image Grid.Column="2" Stretch="None">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source" Value="..\..\Images\OK.png" />
            <Style.Triggers>
                <DataTrigger Value="True" Binding="{Binding TimeSheet, Converter={StaticResource overQuotaConv}}">
                    <Setter Property="Source" Value="..\..\Images\Error.png"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>
Arlinda answered 10/3, 2010 at 3:42 Comment(3)
suh-weet this stuff is coming in byte sized bits to me so far. thanks!Amorphism
multiple Hi 5's...i don't know why this was so hard to do, but your example helped me allot.Rowney
To me it was the usage of the Setter for the standard value. Setting the image source as a property of the image disabled the style somehow. I'm building for .NET 6.0, everything is slightly different here.Pyroelectricity

© 2022 - 2024 — McMap. All rights reserved.