简体   繁体   English

删除按钮周围的边框

[英]Remove Border around Button

I've been trying to remove the Border around a Button and also change the Colour of the Button 's Background when the mouse hovers over it. 我一直在尝试删除Button周围的Border ,并在鼠标悬停在Button上方时更改Button BackgroundColour

Here is what I have: 这是我所拥有的:

<Button Name="Home" Content="Home" HorizontalAlignment="Left" Width="75" Click="Button_Click_Home" Background="#FF252525" FontFamily="/VideoManager;component/#Myriad Pro" FontSize="13.333" Foreground="White" BorderThickness="5">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
           <Setter Property="Template">
             <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                   <Border BorderThickness="0"/>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
            <Style.Triggers>
               <Trigger Property="IsMouseOver" Value="True">
                  <Setter Property="Background" Value="#FF36290A"/>
               </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

The issue is that regardless of what I set BorderThickness to, the Button disappears. 问题是,无论我将BorderThickness设置为什么,该Button消失。 Also the Button is not changing to the colour I specified with the Trigger . 而且Button不会更改为我使用Trigger指定的颜色。

I also tried simply using a Style Setter but found this had no effect on my Button . 我还尝试仅使用Style Setter但发现这对我的Button没有影响。

<Style TargetType="{x:Type Button}">
    <Setter Property="BorderThickness" Value="0"/>
</Style>

You need to define following ControlTemplate and remove Style Trigger. 您需要定义以下ControlTemplate并删除样式触发器。

<ControlTemplate TargetType="{x:Type Button}">
    <Border Name="border"
            Background="{TemplateBinding Background}">
        <ContentPresenter HorizontalAlignment="Center"
                          VerticalAlignment="Center" />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver"
                 Value="True">
            <Setter TargetName="border"
                    Property="Background"
                    Value="#FF36290A" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

I have tried this and this is working. 我已经尝试过了,这正在工作。

I have a style that I use to remove the default Windows style from buttons ; 我有一种样式,用于从按钮中删除默认的Windows样式; I'm sure you can use it or use it to achieve what you want. 我确定您可以使用它或使用它来实现您想要的。 As it is, it just makes a button with absolutely no border, no background, no margins, nothing, just play with the template to achieve what you want : 实际上,它只是制作了一个绝对没有边框,没有背景,没有空白,没有任何东西的按钮,只需玩模板即可实现所需的功能:

<Style x:Key="NoChromeButton" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                            <Setter Property="Opacity" TargetName="Chrome" Value="0.5"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Cursor" Value="Hand"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

It is not mine but I don't remember where I found it, so sorry if the author come by ! 它不是我的,但我不记得在哪里找到的,如果作者来了,对不起!

You could also take the default button template from MSDN and tweak it. 您也可以从MSDN中获取默认按钮模板并进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM