Can my WPF Style Setter use a TemplateBinding?
Asked Answered
R

3

26

I'm trying to do something like this...

<Style
    x:Key="MyBorderStyle"
    TargetType="Border">
    <Setter
        Property="Padding"
        Value="{TemplateBinding Padding}" />
</Style>

...but I get the error:

'Padding' member is not valid because it does not have a qualifying type name.

How do I provide a "qualifying type name"?

Note: The reason I'm trying to do this, is that I'd like to include the same Border in a series of similar ControlTemplates.

I also tried this:

<Setter
    Property="Padding"
    Value="{TemplateBinding GridViewColumnHeader.Padding}" />

...and it actually compiled, but then when I ran the app, I got a XamlParseException:

Cannot convert the value in attribute 'Value' to object of type ''.

I thought maybe qualifying Padding with GridViewColumnHeader (which is the ControlTemplate I want to use this style with) would work, but no dice.

EDIT:

Well, according to the documentation for TemplateBinding, it says:

Links the value of a property in a control template to be the value of some other exposed property on the templated control.

So it sounds like what I'm trying to do is just plain impossible. I really would like to be able create reusable styles for certain controls in my control templates, but I guess the template bindings cannot be included in these styles.

Romeu answered 29/8, 2009 at 14:58 Comment(0)
A
42

TemplateBinding should work for the case where you're templating a control and you want to bind the value of a property of that control to a property of a different control inside the template. In your case you're templating something (call it MyControl), and that template will include a border whose Padding should be bound to MyControl's padding.

From MSDN documentation:

A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource={RelativeSource TemplatedParent}}.

But for whatever reason, specifying TemplatedParent as the source for the binding doesn't seem to work within Style Setters. To get around that you can specify the relative parent to be an AncestorType of the control you're templating (which effectively finds the TemplatedParent providing you haven't embedded other MyControls in the MyControl template).

I used this solution when I was trying to custom template a Button control in which the (String) Content of the Button needed to be bound to the Text property of a TextBlock in the ControlTemplate for the button. Here's what that code looked like:

<StackPanel>
    <StackPanel.Resources>
        <ControlTemplate x:Key="BarButton" TargetType="{x:Type Button}">
            <ControlTemplate.Resources>
                <Style TargetType="TextBlock" x:Key="ButtonLabel">
                    <Setter Property="Text" Value="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type Button}} }" />
                </Style>
            </ControlTemplate.Resources>
            <Grid>
                <!-- Other controls here -->
                <TextBlock Name="LabelText" Style="{StaticResource ButtonLabel}" />
            </Grid>
        </ControlTemplate>
    </StackPanel.Resources>
    <Button Width="100" Content="Label Text Here" Template="{StaticResource BarButton}" />
</StackPanel>
Amandie answered 22/6, 2010 at 16:46 Comment(0)
L
6

The {TemplateBinding ...} shortcut is not available in a Setter.

But nobody will stop you using the full verbose version such as:

Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Padding}".

Lucinalucinda answered 12/2, 2020 at 18:17 Comment(0)
E
4

A property can be qualified simply by prefixing it with the type name. For example, Border.Padding instead of Padding.

However, I'm not sure it makes sense for your scenario. TemplateBindings are used inside a control template.

Entoblast answered 29/8, 2009 at 15:36 Comment(1)
Thanks, @Kent. Your answer gave me an idea to try out (see my edit above), but it didn't work. It makes sense that TemplateBindings can only be used within a ControlTemplate...if only I could convince the parser that I intend to use this style only within a ControlTemplate...Romeu

© 2022 - 2024 — McMap. All rights reserved.