[WPF series-Advanced TemplateBinding vs RelativeSource TemplatedParent]

Source: Internet
Author: User

What's the difference between these 2 bindings:

<ControlTemplateTargetType="{x:type Button}">   <BorderBorderBrush="{TemplateBinding Property=background}">      <ContentPresenter/>   </Border></ControlTemplate>

and

<ControlTemplateTargetType="{x:type Button}">   <BorderBorderBrush="{Binding Relativesource={relativesource templatedparent}, Path=background}">      <ContentPresenter/>   </Border></ControlTemplate>

TemplateBinding -more limiting than using regular Binding

    • More efficient than a Binding but it had less functionality
    • Only works inside a ControlTemplate ' s visual tree
    • Doesn ' t work with properties on Freezables
    • Doesn ' t work within a ControlTemplate ' s Trigger
    • Provides a shortcut in setting properties (not as verbose), e.g. {TemplateBinding TargetProperty}

Regular Binding -Does not having above limitations of TemplateBinding

    • Respects Parent Properties
    • Resets Target values to clear out any explicitly set values
    • Example: <ellipse fill= "{Binding relativesource={relativesource templatedparent},path=background}"/>

One more thing-templatebindings don ' t allow value converting. They don ' t allow you to pass a Converter and don't automatically convert int to string for example (which are normal for a Binding).

Templetebinding is a shorthand for binding with templatedparent but it does not expose all the capabilities of the binding Class, for example you can ' t control Binding.mode from Templetebinding.

Reference WPF TemplateBinding vs RelativeSource TemplatedParent

[WPF series-Advanced TemplateBinding vs RelativeSource TemplatedParent]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.