문제

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.

도움이 되었습니까?

해결책

How about this:

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

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top