Transparent button background in WPF using style

前端 未结 4 1533
-上瘾入骨i
-上瘾入骨i 2021-02-14 17:32

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.

Soluti

相关标签:
4条回答
  • 2021-02-14 17:36

    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);
    
    0 讨论(0)
  • 2021-02-14 17:39

    Change this line

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

    to

    <Setter Property="Background" Value="Transparent"/>
    
    0 讨论(0)
  • 2021-02-14 17:49

    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>
    
    0 讨论(0)
  • 2021-02-14 17:58

    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>
    

    0 讨论(0)
提交回复
热议问题