Frage

Suppose I have the following:

<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type Button}">
   <Grid>
      <Rectangle Width="100" Height="20" />
   </Grid>
</ControlTemplate>

<Button Template="MyButtonTemplate" Width="25" />
<Button Template="MyButtonTemplate" Width="50" />
<Button Template="MyButtonTemplate" Width="75" />

What do I need to do to the control template to have the rectangle be the width of the button instead of a hard-coded 100 each time? Is there a way for me to access the property value of my target type from inside of my control template?

Thanks.

War es hilfreich?

Lösung

How about this:

 <Rectangle Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" />

Andere Tipps

Don't specify Width or Height for Rectangle at all and let it stretch to the size of the Button:

<ControlTemplate TargetType="{x:Type Button}">
   <Grid>
      <Rectangle/>
   </Grid>
</ControlTemplate>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top