Search code examples
xamlwin-universal-appxaml-binding

Is there a shorthand for RelativeSource={RelativeSource Mode=TemplatedParent}?


Specifying the templated parent as the source for a binding in a templated control seems common but irritatingly verbose. Is there a shorthand way of doing this?

Some sort of prefix on the path would seem more reasonable. e.g.

{Binding ^Title}

or something like that, instead of

{Binding Title, RelativeSource={RelativeSource Mode=TemplatedParent}}

Solution

  • You could use a TemplateBinding:

    {TemplateBinding Title}
    

    Note however that

    The types used as the value for the source property and the target property must match. There's no opportunity to introduce a converter when you're using TemplateBinding. Failing to match values results in an error when parsing the XAML. If you need a converter you can use the verbose syntax for a template binding such as: {Binding RelativeSource={RelativeSource TemplatedParent}, Converter="..." ...}

    and, probably more important, the second sentence here:

    A TemplateBinding is always a one-way binding. Both properties involved must be dependency properties.