使用Viewbox可以拉伸和縮放單個子元素以填滿可用空間。
把viewbox直接放在默認(rèn)格式的button外面
<Viewbox>
<Button>left</Button>
</Viewbox>
默認(rèn)格式的button
放在圓角button外:
<ControlTemplate x:Key="radiusBtnTemplate" TargetType="Button">
<Border CornerRadius="48" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
圓角button
此時button中的文字雖然隨著屏幕放大而變大叼旋,但是文字已經(jīng)超出了圓角button顯示區(qū)域剧辐,并不美觀圆兵。
當(dāng)我們把viewbox加在ContentPresenter外時骇陈,同樣的CornerRadius獲得的button:
viewbox加在ContentPresenter外
如果對button內(nèi)容和邊框的間隔不滿意念颈,還可以通過padding或者加一個TextBlock來進(jìn)行調(diào)節(jié):
ContentPresenter外加一個TextBlock
<ControlTemplate x:Key="radiusBtnTemplate" TargetType="Button">
<Border CornerRadius="48" Name="PART_Background" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Viewbox>
<TextBlock Margin="10">
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</TextBlock>
</Viewbox>
</Border>
</ControlTemplate>