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 , , .
?