Metro XAML - Problems with TemplateBinding and SolidColorBrush

Here is a simple user control to illustrate my problem.

public sealed class TestControl : Control
{
    public static DependencyProperty TestColorProperty = DependencyProperty.Register("TestColor", typeof(Brush), typeof(TestControl), new PropertyMetadata(new SolidColorBrush(Colors.Blue)));

    public Brush TestColor
    {
        get { return (Brush)GetValue(TestColorProperty); }
        set { SetValue(TestColorProperty, value); }
    }

    public TestControl()
    {
        this.DefaultStyleKey = typeof(TestControl);
    }
}

As you can see, it has one dependency property Brush, with a default value Blue(set to PropertyMetaData, as shown above.

Here is XAML for my management in Generic.xaml

<Style TargetType="local:TestControl">
        <Setter Property="TestColor" Value="Red" />
        <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TestControl">
                <Border
                    Background="{TemplateBinding TestColor}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBlock Text="TEST"  />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

As you can see, I set the TestColorBrush dependency property to Red in the setting Style- overriding the default value of Blue, as indicated in my PropertyMetaData. Note that my border in my template uses TemplateBindingto set the background to the brush, as discussed.

So, what color do you think is set against the background of the border? Red or blue?

The answer will not be.

-, (, OnApplyTemplate ), , ( ), . , , ProprtyMetaData .

( "" . , SolidColorBrush - .

public BlankPage()
{
    this.InitializeComponent();
    testcont.TestColor = new SolidColorBrush(Colors.Orange);
}

:

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
    <local:TestControl  TestColor="Green" />
</Grid>

TemplateBinding , , .

?

+5
5

, - xaml. - , :

    <Setter Property="SelectedDayBrush">
        <Setter.Value>
            <SolidColorBrush>#7F7F7F7F</SolidColorBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="SelectedDayBrush">
        <Setter.Value>
            <SolidColorBrush Color="Orange"/>
        </Setter.Value>
    </Setter>
0

, . :

<SolidColorBrush x:Key="DefaultTestColorBrush">Red</SolidColorBrush>

:

<Setter Property="TestColor" Value="{StaticResource DefaultTestColorBrush}" />

.

0

, . - , PointerOver - , , , - PointerOverVisual . PointerOver . , :

<VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames   Storyboard.TargetProperty="Opacity" Storyboard.TargetName="pointerOverVisual">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="0.15"/>
                                    </ObjectAnimationUsingKeyFrames>

.......

<Border x:Name="Border" BorderThickness="0" Background="{TemplateBinding Background}" Margin="3">   
        content here                                                    
</Border>
    <Rectangle x:Name="pointerOverVisual" Fill="White" Opacity="0" Margin="3"/>
    <Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/>
    <Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="0.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/>
0

, , , , ( ).

( generic.xaml, , )

<Style TargetType="local:myPad">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:myPad">
                <Path Data="..." Height="60" Width="30" Fill="{TemplateBinding myPadColor}"/>

( myPad.cs)

    public sealed class myPad : Control
    {
        public myPad()
        {
            this.DefaultStyleKey = typeof(myPad);
        }

        public SolidColorBrush myPadColor
        {
            get { return (SolidColorBrush)GetValue(PadColorProperty); }
            set { SetValue(PadColorProperty, value); }
        }

        public static readonly DependencyProperty PadColorProperty =
            DependencyProperty.Register("myPadColor", typeof(SolidColorBrush), typeof(myPad), new PropertyMetadata(null));
}

, "" ...

<UserControl
    x:Class=....
    <Canvas Width="235" Height="235">
        <local:myPad x:Name="thisPad" myPadColor="White">

...

(...), , . , myPadColor .

btw, VS2012 SP3.. , , .

also, I'm pretty new to XAML programming, but on the Tim Heuer blog I found the right way to make the dependency property (thanks Tim!)

if you replace the (null) value in the propertymetadata value with yours (the new SolidColorBrush (Colors.Black)), you will get the default value in development mode.

:)

hope this helps.

0
source

You can try the code snippet below to bind TestColor,

 Background="{Binding  RelativeSource={RelativeSource TemplatedParent}, Path=TestColor}"

I hope this will be helpful.

Regards, Rex

0
source

All Articles