xaml模板按钮

时间:2017-04-02 11:29:05

标签: wpf xaml templates button viewbox

我无法在模板化按钮中居中文本。 “OK”停留在底部中央,甚至低于按钮边框。

 <Window.Resources>
    <ControlTemplate x:Key="BT_Kids" TargetType="{x:Type Button}">
        <Grid>
            <Ellipse HorizontalAlignment="Left" Height="44.8" Stroke="Black" VerticalAlignment="Top" Width="71.2">
                <Ellipse.Fill>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="#FF4363EA" Offset="1"/>
                    </LinearGradientBrush>
                </Ellipse.Fill>
            </Ellipse>
            <Viewbox>
                <ContentControl Margin="10" Content="{TemplateBinding Content}"/>
            </Viewbox>
        </Grid>
    </ControlTemplate>
</Window.Resources>
<Grid Margin="0,0,0,2.4">

    <Button 
            HorizontalAlignment="Left" 
            VerticalAlignment="Top" 
            Width="75" Margin="189.6,24.6,0,0" 
            Template="{DynamicResource BT_Kids}"  FontSize="15">OK</Button>

</Grid>

2 个答案:

答案 0 :(得分:1)

移除ViewBox,调整FontSize的{​​{1}},并将Button的{​​{1}}和HorizontalAlignment属性设置为{{ 1}}:

VerticalAlignment

enter image description here

ContentPresenter具有固定大小时,将内容放在Center中是没有意义的。

答案 1 :(得分:0)

试试这个:

<Button ...>
  <Button.Content>
            <StackPanel Padding="0" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="OK"/>
            </StackPanel>
        </Button.Content>
</Button>