简体   繁体   English

在WPF的透明按钮背景使用样式

[英]Transparent button background in WPF using style

I've found this topic about setting the border of a button to transparent. 我找到了关于将按钮的边框设置为透明的主题。 This works fine, but I want to use this for the button background and not the border. 这工作正常,但我想将此用于按钮背景而不是边框​​。

Solution in link which I've put in <Window.Resources>...</Window.Resources> : 链接中的解决方案,我已经放入<Window.Resources>...</Window.Resources>

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Green"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

Source: How do you change Background for a Button MouseOver in WPF? 来源: 如何在WPF中更改Button MouseOver的背景?

How can edit this code so I can use this to set the background of my button to transparent and not the border of it? 如何编辑这段代码,以便我可以使用它来将我的按钮的背景设置为透明而不是它的边框?

Try this snippet for a transparent button: 尝试使用此代码段获取透明按钮:

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="0">
    <-- PUT BUTTON CONTENT HERE e.g. a Image -->
</Button>

Found the solution for my issue: 找到我的问题的解决方案:

Apparently you have to add the triggers within the ControlTemplate under ControlTemplate.Trigger. 显然,您必须在ControlTemplate.Trigger下的ControlTemplate中添加触发器。 Andd after you've done that the thing with borders is that you have to set a TargetName in the Border tag and then set the reference (-> TargetName="XXXXX") to the properties which you've named in the border tag. Andd在你完成之后,带边框的东西是你必须在Border标签中设置TargetName,然后将引用( - > TargetName =“XXXXX”)设置为你在border标签中命名的属性。

So: 所以:

<Window.Resources>
<Style x:Key="MenuButton" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Height" Value="40" />
    <Setter Property="Width" Value="Auto" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Margin" Value="45,0,0,0" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                <Border Name="MenuBorder" SnapsToDevicePixels="True" BorderBrush="Black" Background="{TemplateBinding Background}" BorderThickness="0,0,0,2" >
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="Button.IsFocused" Value="True">
                        <Setter Property="Background" Value="Transparent"/>
                        <Setter TargetName="MenuBorder" Property="BorderBrush" Value="#FFED6A2B" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Change this line 改变这一行

<Setter Property="Background" Value="Red"/>

to

<Setter Property="Background" Value="Transparent"/>

Use this in C# code 在C#代码中使用它

Button btn = new Button() {BorderBrush=System.Windows.Media.Brushes.Transparent, 
                               BorderThickness = new Thickness(0)};

or 要么

yourBtn.BorderBrush=System.Windows.Media.Brushes.Transparent;
yourBtn.BorderThickness =new Thickness(0);

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

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