Linking TemplatedParent with two levels

I am sure that this has been resolved before, but I cannot find the right solution that knows correctly. Maybe I just don't know the terms I'm looking for.

Assuming I have this user control pattern

<Style x:Key="ColorPicker" TargetType="{x:Type local:ColorPicker}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ColorPicker}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Thumb Width="30" Height="30" Canvas.Left="0" Canvas.Top="0">
                        <Thumb.Style>
                            <Style TargetType="Thumb">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Ellipse Fill="{TemplateBinding SelectedColor}" Width="30" Height="30" Stroke="Black" StrokeThickness="1" />
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Thumb.Style>
                    </Thumb>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

where SelectedColoris the ColorPicker property. In the above example, the template binding will look SelectedColorin the parent type template Thumb, however, how can I get the binding to the second level parent template?

+4
source share
1 answer
Fill="{Path=SelectedColor, RelativeSource={RelativeSource FindAncestor, AncestorType={local:ColorPicker}}}"

ColorPicker ColorPickers, Thumb. , - TemplateBinding. customcontrols, TemplateBinding!

:)

<Style x:Key="ColorPicker" TargetType="{x:Type local:ColorPicker}">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:ColorPicker}">
            <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                <Thumb Width="30" Height="30" Canvas.Left="0" Canvas.Top="0">
                    <Thumb.Style>
                        <Style TargetType="Thumb">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Ellipse Fill="{Path=SelectedColor.Color, RelativeSource={RelativeSource FindAncestor, AncestorType={local:ColorPicker}}}" Width="30" Height="30" Stroke="Black" StrokeThickness="1" />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Thumb.Style>
                </Thumb>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

WPC Cheatsheet - , !

,

+5

All Articles