我找到了关于将按钮的边框设置为透明的主题。 这样可以正常工作,但我想将它用于按钮背景而不是边框。
我在<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>
来源:How do you change Background for a Button MouseOver in WPF?
如何编辑此代码以便我可以使用此代码将我的按钮背景设置为透明而不是它的边框?
答案 0 :(得分:7)
尝试使用此代码段获取透明按钮:
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="0">
<-- PUT BUTTON CONTENT HERE e.g. a Image -->
</Button>
答案 1 :(得分:6)
找到我的问题的解决方案:
显然你必须在ControlTemplate.Trigger下的ControlTemplate中添加触发器。 Andd在你完成之后,有边框的东西是你必须在Border标签中设置TargetName,然后将引用( - &gt; TargetName =“XXXXX”)设置为你在border标签中命名的属性
所以:
<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>
答案 2 :(得分:5)
更改此行
<Setter Property="Background" Value="Red"/>
到
<Setter Property="Background" Value="Transparent"/>
答案 3 :(得分:0)
在C#代码中使用它
Button btn = new Button() {BorderBrush=System.Windows.Media.Brushes.Transparent,
BorderThickness = new Thickness(0)};
或
yourBtn.BorderBrush=System.Windows.Media.Brushes.Transparent;
yourBtn.BorderThickness =new Thickness(0);