简体   繁体   中英

How to apply a HeaderTemplateProperty to a HeaderProperty inside in a CustomControl?

In the MyCustomControl , I created the Heder and HeaderTemplate dependency properties.

    public static readonly DependencyProperty HeaderProperty =
           DependencyProperty.Register(
                   "Header",
                   typeof(object),
                   typeof(MyCustomControl),
                   new FrameworkPropertyMetadata(
                           (object)null,
                           new PropertyChangedCallback(OnHeaderChanged)));


    public static readonly DependencyProperty HeaderTemplateProperty =
     DependencyProperty.Register(
             "HeaderTemplate",
             typeof(DataTemplate),
             typeof(MyCustomControl),
             new FrameworkPropertyMetadata(
                     (DataTemplate)null,
                     new PropertyChangedCallback(OnHeaderTemplateChanged)));

    private static void OnHeaderTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // ???;
    }

I don't understand what I have to write for HeaderTemplate to apply to Header . I think it needs to be written here OnHeaderTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) , but what exactly?

<Style TargetType="{x:Type local:MyCustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter ContentSource="Header"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

You'll bind that in the control template, probably just like this:

<ContentPresenter 
    ContentSource="Header"
    ContentTemplate="{TemplateBinding HeaderTemplate}"
    />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM