Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to a template

Say I have defined a button with rounded corners.

<Style x:Key="RoundButton" TargetType="Button">
    <!-- bla bla -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="0,5,5,0" />
                <!-- bla bla -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I it possible that the user of this button can specify the CornerRadius? Can I use a TemplateBinding? But where should I bind to? (to Tag?)

like image 668
Robbert Dam Avatar asked Nov 16 '25 18:11

Robbert Dam


2 Answers

In addition to Kent's suggestions, you could also create an attached property to define the CornerRadius on the button, and bind to that property in the template

like image 116
Thomas Levesque Avatar answered Nov 18 '25 09:11

Thomas Levesque


In order to use a TemplateBinding, there must be a property on the templated control (Button, in this case). Button does not have a CornerRadius or equivalent property, so your options are:

  • hard code the value in the template
  • Hijack another property (such as Tag) to store this information. This is quicker, but lacks type safety, is harder to maintain, and prevents other uses of that property.
  • Subclass Button and add the propery you need, then provide a template for that subclass. This takes a little longer but yields a much nicer experience for consumers of your control.
like image 38
Kent Boogaart Avatar answered Nov 18 '25 09:11

Kent Boogaart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!