Wpf 自定义按钮内容对齐
我正在尝试在Wpf 自定义按钮内容对齐,wpf,xaml,button,Wpf,Xaml,Button,我正在尝试在xaml中创建自定义按钮。这很好,但我在内容对齐方面有一些奇怪的问题 如果我在“根网格”中放置一个自定义按钮,则整个按钮都可以单击,并且内容(按钮文本)正确居中对齐 但是,如果我以这种方式放置自定义按钮: 边界 堆垛板 我的按钮 我的按钮 等等 然后,我的按钮的-内容也正确居中,但只能在内容的文本上单击。不是整个按钮 如果我将内容对齐设置为“拉伸”,则可以单击,但文本不居中 这是我的xaml:如何在上面的设置中使整个按钮可点击 控制模板: <ControlTempl
xaml
中创建自定义按钮。这很好,但我在内容对齐方面有一些奇怪的问题
如果我在“根网格”
中放置一个自定义按钮,则整个按钮都可以单击,并且内容(按钮文本)正确居中对齐
但是,如果我以这种方式放置自定义按钮:
- 边界
- 堆垛板
- 我的按钮
- 我的按钮
- 等等
- 堆垛板
<ControlTemplate x:Key="ollema">
<Grid Background="{TemplateBinding Background}" VerticalAlignment="Stretch">
<Border Name="border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</ContentPresenter>
</StackPanel>
</Border>
</Grid>
</ControlTemplate>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Width="200" Background="#FF00641E" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Width="150" HorizontalAlignment="Center" VerticalAlignment="Center">
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="{x:Null}" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0"/>
</StackPanel>
</Border>
我的主窗口中的XAML:
<ControlTemplate x:Key="ollema">
<Grid Background="{TemplateBinding Background}" VerticalAlignment="Stretch">
<Border Name="border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</ContentPresenter>
</StackPanel>
</Border>
</Grid>
</ControlTemplate>
<Border BorderBrush="White" BorderThickness="1" Margin="0" Width="200" Background="#FF00641E" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Width="150" HorizontalAlignment="Center" VerticalAlignment="Center">
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="{x:Null}" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0"/>
</StackPanel>
</Border>
你能试着把
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
对于stackpanel和contentPresenter@DeMama,将
切换按钮中的背景设置为透明
,而不是x:Null
差不多
<Border BorderBrush="White" BorderThickness="1" Margin="0" Width="200" Background="#FF00641E" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Width="150" HorizontalAlignment="Center" VerticalAlignment="Center">
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
<ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="{x:Null}" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0"/>
</StackPanel>
</Border>
刚刚尝试了此操作,即使在ContentPresenter
之外,单击事件也会被正常调用,这是按钮本身的控制模板吗?如果是,请将其内部的ContentControl
切换到ContentPresenter
。设置样式
使背景
的默认设置器
透明
,并将Background=“{TemplateBinding Background}”
添加到网格
@Viv是的,它是控制模板
本身。我试过你的建议,但结果是一样的。我认为这与我的按钮
包含在堆栈面板
中以及我的堆栈面板包含在边框
中有关。我会再多玩一点..那么你能添加完整的xaml代码吗。我不能用你发布的代码复制这个。样式,StackPanel,几乎所有与此相关的xaml内容。@Viv谢谢,我编辑了我的帖子。请注意,我对xaml非常陌生…已经尝试过了,但是我的按钮内容
位于左上方
位置,我需要它位于中间..给你的ContentPresenter添加一些边距或给你的StackPanel添加一些填充太棒了,它就像一个符咒!谢谢你,伙计,我不知道会这么容易:)@DeMama,欢迎你。大多数问题总是有简单的解决办法。就要找到他们:)