Skip to main content

Microsoft Silverlight

Answered Question TemplateBinding not working for properties of type ColorRSS Feed

(0)

ihatescarves
ihatesca...

Member

Member

18 points

11 Posts

TemplateBinding not working for properties of type Color

I originally posted this in the Controls forum but didn't get any replies to say that I was doing anything wrong, so I suppose that means it's probably a bug.

The problem is that TemplateBinding doesn't work with properties of type Color.  In the below example, where both the propertyName and targetProperty are dependency properties (as http://msdn.microsoft.com/en-us/library/cc189062(VS.95).aspx says that need to be), the resulting Color of the SolidColorBrush is #00000000 (an empty Color) rather than Magenta, which is the value of the BorderColour property.

To test this, I created a templated control called TestControl that inherits from Control and has a single property:

    public class TestControl : Control
    {
       public static DependencyProperty BorderColourProperty
           = DependencyProperty.Register("BorderColour", typeof(Color), typeof(TestControl), null);

       public Color BorderColour
       {
            get { return (Color)GetValue(BorderColourProperty); }
            set { SetValue(BorderColourProperty, value); }
        }
    }
 

The template from generic.xaml

    <Style TargetType="src:TestControl">
        <Setter Property="BorderColour" Value="Magenta" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="src:TestControl">
                    <Border BorderThickness="5" CornerRadius="10">
                        <Border.BorderBrush>
                            <SolidColorBrush Color="{TemplateBinding BorderColour}" />
                        </Border.BorderBrush>
                        <TextBlock Text="Give me a border colour!" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

When the control is rendered I get the text with no visible border; if I replace Color="{TemplateBinding BorderColour}" with Color="Magenta", I get the border as expected.

Thanks,

Tim

thierry.bouquain
thierry....

Contributor

Contributor

2054 points

280 Posts

Silverlight MVP
Answered Question

Re: TemplateBinding not working for properties of type Color

SolidColorBrush is not a FrameworkElement. Silverlight 2 beta 1 allows only binding on FrameworkElement. As a workaround, you can create a dependency property that returns a SolidColorBrush instead of a Color and Bind the BorderBrush. 

Thierry Bouquain
Ucaya
http://www.ucaya.com
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities