use*_*er1 0 c# wpf xaml button controltemplate
所以我有一个在wpf中创建的圆形按钮,如下所示:
<Button Grid.Column="3" Width="25" Height="25" Content="X" Margin="10,5,5,5" Foreground="Red" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button.Template>
<ControlTemplate>
<Grid>
<Ellipse Name="Ellipse" Fill="{TemplateBinding Background}"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
Run Code Online (Sandbox Code Playgroud)
但是它显示的按钮是:
为什么红色X没出现在我按钮的中央?并且可以更改我的xaml,以使红色X出现。(ps。灰色背景是由于我资源中的按钮样式)
这是因为您的任何内容都Template
无法显示Content
。为此,您需要使用ContentPresenter
<Button Grid.Column="3" Width="25" Height="25" Content="X" Margin="10,5,5,5" Foreground="Red" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Name="Ellipse" Fill="{TemplateBinding Background}"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
Run Code Online (Sandbox Code Playgroud)
您还需要添加TargetType="{x:Type Button}"
到ControlTemplate