Transparent button background in WPF using style

2019-03-28 06:14发布

问题:

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>:

<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?

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?

回答1:

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>


回答2:

Found the solution for my issue:

Apparently you have to add the triggers within the ControlTemplate under ControlTemplate.Trigger. 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.

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>



回答3:

Change this line

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

to

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


回答4:

Use this in C# code

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);