首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ControlTemplate内边框的大小设置为按钮的大小

将ControlTemplate内边框的大小设置为按钮的大小
EN

Stack Overflow用户
提问于 2021-04-02 16:55:47
回答 1查看 33关注 0票数 0

我想要一个有几个按钮的ControlTemplate,这样当鼠标悬停在按钮上时,我可以控制按钮的颜色。问题是我使用边框作为ControlTemplate内容,并且希望边框的大小(BorderThickness)等于按钮的大小。因为按钮是网格的一部分,所以它的大小是灵活的。这是我到目前为止的代码:

代码语言:javascript
复制
<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, 0, 30" Style="{StaticResource ex}" Grid.Row="0"/>
    <Button Content="Btn1" Margin="0, 10, 0, 30" Grid.Row="1"/>
    <Button Content="Btn2" Margin="0, 10, 0, 30" Grid.Row="2"/> 
</Grid>

如何让BorderThickness填充整个网格行。我应该首先使用边框作为ControlTemplate内容吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-02 17:00:25

你应该直接写

代码语言:javascript
复制
<ControlTemplate TargetType="Button">
    <Border Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}">
        <ContentPresenter
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Border>
</ControlTemplate>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66916846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档