0

I am trying to change an Image's Source property whenever the image is disabled/enabled. I checked this out and that worked, whenever the image is disabled the opacity is changed. However, as soon as I try to set the Source property, it just does not work. It is ignored. That's my markup:

<Image Source="/My_Project;component/Images/countdown.png" Width="75"> <Image.Style> <Style TargetType="Image"> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Source" Value="/My_Project;component/Images/countdown.disabled.png"/> <!-- does not work --> <!-- <Setter Property="Opacity" Value="0"/> this works! --> </Trigger> </Style.Triggers> </Style> </Image.Style> </Image> 

Am I missing something? Is it even possible to change the Source property?

2 Answers 2

2

Your problem is that you have 'hard coded' the Image.Source value when you declared your Image. You need to move that to the Style too. Try this:

<Image Width="75"> <Image.Style> <Style TargetType="Image"> <Setter Property="Source" Value="/My_Project;component/Images/countdown.png"/> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Source" Value="/My_Project;component/Images/countdown.disabled.png"/> </Trigger> </Style.Triggers> </Style> </Image.Style> </Image> 

The reason for this is because WPF has many different ways to update a DependencyProperty and so has a Dependency Property Value Precedence list which specifies which sources should be able to override changes made from other sources. According to this list, a local set value will override one set in a Style Trigger, but one set in a Style Trigger will override one set in a Style Setter.

Please refer to the linked page for further information on this.

Sign up to request clarification or add additional context in comments.

2 Comments

I like your code formatting that avoids horizontal scrollbars :-)
We could do with automatic text wrapping on this website... it's well overdue. :)
2

Due to Dependency Property Value Precedence this will only work if you do not set a local value for the Source property, but instead also set it via the Style:

<Image Width="75"> <Image.Style> <Style TargetType="Image"> <Setter Property="Source" Value="/My_Project;component/Images/countdown.png"/> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Source" Value="/My_Project;component/Images/countdown.disabled.png"/> </Trigger> </Style.Triggers> </Style> </Image.Style> </Image> 

5 Comments

LOL! Are we writing the same answer here? +1 for also mentioning the Dependency Property Value Precedence.
Great, thanks! @Sheridan sorry I can't mark your answer as accepted too, I just opted for the first one chronically :) Thanks to both of you!
@DavideDeSantis, I am more than happy for you to award the accepted answer to whomever you like, but for future reference only, we're supposed to award it to the best answer, not the first answer.
@Sheridan I know, and of course you are right. I was just thinking that you had basically written the same thing, but I read it again and your answer is a little bit more detailled than Clemens', so here you go, corrected :)
I didn't mean for you to change the accepted answer on this question... that's why I put for future reference only in my comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.