Yes, the "local value" in the priority list applies only to properties set on elements outside the templates. The relevant part of the priority list is 4b:
4. Template Properties TemplatedParent . An element has a TemplatedParent if it was created as part of a template (ControlTemplate or DataTemplate). For details on when this is applicable, see TemplatedParent later in this thread. As part of the template, the following priority applies:
and. Triggers from TemplatedParent template.
b. Property sets (usually through XAML Attributes) in the TemplatedParent Template.
, , , , , .
, , DependencyPropertyHelper.GetValueSource BaseValueSource. , , "", "ParentTemplate".
, . , , ClearValue, , ,
, , . UserControl . , , - . "", , . "", . "Display", ( , ParentTemplate, ).
XAML:
<UserControl
x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Template>
<ControlTemplate>
<StackPanel Background="Transparent">
<Button Click="Display_Click" Content="Display"/>
<Button Click="Set_Click" Content="Set"/>
<Button Click="Clear_Click" Content="Clear"/>
<Rectangle Width="100" Height="100"
Fill="Blue" Name="PART_Rectangle"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_Rectangle"
Property="Fill" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Template>
</UserControl>
Code-:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
rectangle = Template.FindName("PART_Rectangle", this) as Rectangle;
}
private Rectangle rectangle;
private void Display_Click(object sender, RoutedEventArgs e)
{
var source = DependencyPropertyHelper.GetValueSource(
rectangle, Rectangle.FillProperty);
MessageBox.Show(string.Format("Value {0}; Source {1}",
rectangle.Fill, source.BaseValueSource));
}
private void Set_Click(object sender, RoutedEventArgs e)
{
rectangle.Fill = Brushes.Red;
}
private void Clear_Click(object sender, RoutedEventArgs e)
{
rectangle.ClearValue(Rectangle.FillProperty);
}
}