Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug TemplateBinding not working for properties of type Color
1 replies. Latest Post by thierry.bouquain on May 1, 2008.
(0)
ihatesca...
Member
18 points
11 Posts
04-30-2008 7:50 PM |
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,
thierry....
Contributor
2054 points
280 Posts
05-01-2008 3:59 AM |
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.