1

In the below code, MousePressImage is a dependency property of class ButtonControl. The following Binding doesn't work. Appreciate your help in solving this issue.

Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=MousePressImage}"/> <Style TargetType="{x:Type local:ButtonControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:ButtonControl}"> <Border> <Image x:Name="img" Source="pack://application:,,,/Recipe_06_13;component/Resources/normal.bmp" /> </Border> <!--<Border x:Name="border"> <Border.Background> <ImageBrush x:Name="img" ImageSource="/Recipe_06_13;component/Resources/fatal.png"/> </Border.Background> </Border>--> <ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="img" Property="Source" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=MousePressImage}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

I create the ButtonControl like this.

<local:ButtonControl Height="48" Width="160" MouseOverImage="pack://application:,,,/Recipe_06_13;component/Resources/Over.bmp" MousePressImage="pack://application:,,,/Recipe_06_13;component/Resources/Press.bmp" DisableImage=" "> </local:ButtonControl> 
2
  • What object has the property 'Value'? Note that this Value property will need to be a dependency property in order for the Binding to work. MousePressImage actually doesn't have to be a dependency property, though that simplifies the property changed notification. Commented Mar 27, 2010 at 20:34
  • Dan,Thanks for your reply.. The value is for the property 'Source', I think Source is a dependency property...see below. However, the binding still doesn't work... Am I missing something? <Border> <Image x:Name="img" Source="pack://application:,,,/Recipe_06_13;component/Resources/normal.bmp" /> </Border> Commented Mar 27, 2010 at 20:39

1 Answer 1

6

Because your trigger is on a ControlTemplate, you need to be getting the MousePressImage from the control instance being templated. To do this, use TemplateBinding or (more reliably) RelativeSource TemplatedParent:

<Setter TargetName="img" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MousePressImage}" /> 
Sign up to request clarification or add additional context in comments.

1 Comment

IsMouseOver shouldn't result in flicker, because it shouldn't change except when the mouse-over status changes. You might want to work up a simple repro for this and post it as a separate question -- that will probably get more attention and better answers than a discussion in comments! Also, I think you may have mistaken me for someone else -- I've never worked at Solyndra -- sorry!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.