WPF Dependency Property Property - Template Parent

In the list of sources of values ​​of dependency values, along with relative priority, the local value is the highest priority for determining the base value.
Right after the local value are the template properties from the template parent - for example. The parent ControlTemplate for the control created in the template context.

My question is this: since the local value is specified as priority over the template property, does this mean that the property value set explicitly (local value) for the control created in the template takes precedence over one set using the property trigger in this template ? This is similar to what the rules imply, but the properties set using the triggers in the control template appear to override (that is, a higher priority) the local values ​​set in the template.

Or does the "local value" in the priority list refer to values ​​set only for controls not created in the templates, and therefore you cannot compare the priority between the local value and the set of properties using a trigger in the parent template?

+3
source share
1 answer

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);
    }
}
+4

Source: https://habr.com/ru/post/1776006/


All Articles