[WPF] button按键的透明设置
程序员文章站
2022-07-14 13:17:16
...
WPF 如何将button始终设置为透明色,实际效果如下:
button为透明效果的设定,需要先将button的背景设置为透明,然后在鼠标移动到button上之后也设置为透明;
具体方法如下:
1,在MainWindow.xaml中添加自定义style功能;
<Window.Resources>
<Style x:Key="MyButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="0" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.55" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
2,在button中调用 style属性 注意此处用 DynamicResource前置属性;
<Button x:Name="actbt" Style="{DynamicResource MyButton}" Content="开始" Height="60" Margin="0,0,0,100" VerticalAlignment="Bottom" RenderTransformOrigin="0.481,0.544" Click="actbt_Click" Background="{x:Null}" BorderBrush="{x:Null}" FontFamily="STKaiti" FontSize="24" Foreground="Yellow" FontWeight="Bold" Width="120"/>
参考连接:https://blog.csdn.net/joycesunny/article/details/91125911
【好文共赏析!】