WPF Button IsMouseOver Background

https://stackoverflow.com/questions/17259280/how-do-you-change-background-for-a-button-mouseover-in-wpf

<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>

 

转载于:https://www.cnblogs.com/Fred1987/p/9692219.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中,Button 控件默认情况下是没有鼠标和按键的感觉的,但是你可以通过修改样式来实现在 Button 上显示鼠标和按键的感觉。 以下是一种常见的实现方式: 1. 在 WPF 应用程序中打开 MainWindow.xaml 文件。 2. 在 Window 标签中添加以下属性: xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 3. 在 Window 中添加以下样式: <Window.Resources> <Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true"> <Grid> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}" RecognizesAccessKey="True"/> </Grid> </Microsoft_Windows_Themes:ButtonChrome> <Rectangle x:Name="ButtonOverlay" Opacity="0" Fill="#FF000000"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="ButtonOverlay" Property="Opacity" Value="0.2"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="ButtonOverlay" Property="Opacity" Value="0.4"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> 4. 在 Button 控件中应用样式: <Button Style="{StaticResource ButtonStyle}" Content="Button"/> 这样就可以在 Button 控件上显示鼠标和按键的感觉了。需要注意的是,这种方式需要使用 PresentationFramework.Aero 库,如果你的应用程序中没有引用该库,需要先添加该库的引用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值