使用 Viewbox 控件,您可以调整按钮中内容的大小,因为 ViewBox 旨在填充可用空间而不管大小。
第一种方法
<Button Content="Long Text Long Text Long Text Long Text Long Text Long Text" Foreground="Red" Height="30">
<Button.ContentTemplate>
<DataTemplate>
<Viewbox >
<TextBlock Margin="5,0,5,0" Text="{Binding Path=Content,RelativeSource={RelativeSource AncestorType={x:Type Button}}}" ></TextBlock>
</Viewbox>
</DataTemplate>
</Button.ContentTemplate>
</Button>
注意:您也可以使用 x:key 将此 DataTemplate 存储在资源中,然后分配给按钮 <Button ContentTemplate={StaticResource DatatemplateKeyname}/>
第二种方法
<Button Height="30" Width="200" Foreground="Red">
<Button.Content>
<Viewbox>
<TextBlock Margin="5,0,5,0" Text="Long Text Long Text Long Text Long Text Long Text Long Text" ></TextBlock>
</Viewbox>
</Button.Content>
</Button>
注意:
您可以在此处使用 Tag 属性来存储内容,然后绑定到 viewbox 中的文本块 TextBlock Text="{Binding Path=Tag,RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
第三种方法
<!--Here Width is store in Button "Tag" property.-->
<Viewbox Height="30" Width="{Binding ElementName=Buttonname,Path=Tag}">
<Button x:Name="Buttonname" Content="Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text" Tag="200" Foreground="Red"/>
</Viewbox>