在使用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.
解决方案中的链接,我已经把< Window.Resources> ...< /Window.Resources>
:
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>
来源:鼠标悬停按钮背景
如何修改此code,所以我可以用它来为我设置按钮,透明的背景和它没有边界?
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?
推荐答案
找到了我的问题解决方案:
Found the solution for my issue:
显然,你必须ControlTemplate.Trigger下的ControlTemplate中添加触发器。和D你这样做之后,与边框的事情是,你必须设置在边境标签的的TargetName,然后设置参考( - >的TargetName =XXXXX),将您已在边境标签命名的属性。
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.
所以:
<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>
这篇关于在使用WPF透明样式按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!