Sunday, September 2, 2012

Bind rectangle's Fill color to Foreground color

Bind rectangle's Fill color to Foreground color

I have a custom control inside a button. In the control's template, I have a rectangle, and a text block. When the button is pressed, VSM animates the button's "(Button.Foreground).(SolidColorBrush.Color)" property that's propagated to a text block so the color of the text changes.

How can I set the rectangle's fill property to the same brush?

I tried "{TemplateBinding Foreground}", "{Binding Path=Foreground, RelativeSource={RelativeSource Self}}", and even creating the bindings in code (in OnApplyTemplate) - either not works at all, or the initially set color is not animated. I also tried the element to element binding to bind the rectangle's Fill property to the TextBlock's Foreground property, same result (the initial color is OK, while animating the colors are different).

Thanks in advance.

Answers & Comments...

Answer: 1

I think it might be a bug in the WP 7.1.

Found the workaround. To make it all work:

  • In the custom control template, specify Fill="{TemplateBinding Foreground}"
  • In the button template, inside <VisualState x:Name="Normal">, set the foreground color for the normal state.

When the initial Foreground value comes from <Setter Property="Foreground" ... in the button's style, when the visual state manager animates the foreground value, TextBlock objects receive the new value, however the <Rectangle Fill="{TemplateBinding Foreground}" stays painted with the old color.

When initial value comes from the visual state manager, the rectangle inside custom control template is correctly receiving new property values as the VSM animates color change.

by : Soontshttp://stackoverflow.com/users/126995




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog