问题描述
我想要几个按钮的 ControlTemplate,这样我就可以在鼠标悬停在按钮上时控制按钮的颜色。问题是我使用边框作为 ControlTemplate 内容,并且我希望该边框的大小 (BorderThickness) 等于按钮的大小。由于按钮是网格的一部分,它的大小是灵活的。到目前为止,这是我的代码:
<Window x:Class="WPFTesting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFTesting"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="350">
<Window.Resources>
<Style TargetType="Button" x:Key="ex">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="50"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="Btn0" Margin="0,10,30" Style="{StaticResource ex}" Grid.Row="0"/>
<Button Content="Btn1" Margin="0,30" Grid.Row="1"/>
<Button Content="Btn2" Margin="0,30" Grid.Row="2"/>
</Grid>
如何使 BorderThickness 填充整个网格行。我什至应该首先使用边框作为 ControlTemplate 内容吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)