首页 > 其他分享 >WPF button mouseover background change color

WPF button mouseover background change color

时间:2024-06-07 18:12:17浏览次数:23  
标签:color button background WPF mouseover change

<Application x:Class="WpfApp142.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp142"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="Button">
            <Setter Property="BorderBrush" Value="Blue"/>
            <Setter Property="Background" Value="Yellow"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="border"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="#EE2D2D"/>
                                <Setter TargetName="border" Property="Background" Value="red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>




//main.xaml
<Window x:Class="WpfApp142.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp142"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Width="500" Height="150" Content="Application resources">            
        </Button>
    </Grid>

   
     
</Window>

 

 

 

 

 

 

 

 <Setter Property="Template">
     <Setter.Value>
         <ControlTemplate TargetType="Button">
             <Border x:Name="border"
                     Background="{TemplateBinding Background}"
                     BorderBrush="{TemplateBinding BorderBrush}"
                     BorderThickness="{TemplateBinding BorderThickness}"
                     Padding="{TemplateBinding Padding}">
                 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
             </Border>
             <ControlTemplate.Triggers>
                 <Trigger Property="IsMouseOver" Value="True">
                     <Setter TargetName="border" Property="BorderBrush" Value="#EE2D2D"/>
                     <Setter TargetName="border" Property="Background" Value="red"/>
                 </Trigger>
             </ControlTemplate.Triggers>
         </ControlTemplate>
     </Setter.Value>
 </Setter>

 

标签:color,button,background,WPF,mouseover,change
From: https://www.cnblogs.com/Fred1987/p/18237685

相关文章

  • tkinter的button命令为什么要加lambda标签
    续上一篇,添加一个button打印功能,很意外,怎么我明明没点击button啊,怎么command的代码就运行了呢,这个问题的答案在猿友网解决了,真是匪夷所思,偏要加上lambda:才屏蔽初始化运行。https://www.w3cschool.cn/article/32403450.html其此是打印问题,试了又试,发现通过win32print获取到handl......
  • 如何在Slider中使用MouseLeftButtonDown事件
    MouseLeftButtonDown和MouseLeftButtonUp是两个经常同时使用的事件,最常用到的恐怕就是处理鼠标拖动,这种时候需要在Down事件中CaptureMouse,而在Up事件中Release。但是我在对一个继承Slider的控件这么做的时候却发现MouseLeftButtonUp事件可以正常触发,而MouseLeftButtonDown怎么也......
  • tkinter里使用button更新label图像
    哪怕tkinter是面向玩具的gui界面,想当然的功能也不至于这么迥异啊,而解决的方式是如此的疑无路,再柳暗花明。曾试着将adobereader的默认视图设置为100%,无奈受限于屏幕高度,不管怎样调整设置,都撼不了82.7%,这是真撞墙了,故从昨下午就想着在python上做一个默认100%显示pdf的图形界面,为什......
  • perfers-color-scheme 使用简单介绍
    perfers-color-scheme简介prefers-color-scheme媒体查询属性用于检测用户操作系统是否使用深色模式。属性值dark表示用户操作系统使用深色模式light表示用户操作系统使用浅色模式no-preference表示用户操作系统没有偏好,或者操作系统不支持该属性示例@media(prefers......
  • PyQT5之QPushButton和布局
    fromPyQt5importQtWidgetsfromPyQt5importQtCore,QtGuiimportsysimportcv2defclick_btn1():print("clickbutton1")txt_label.setText("我是按钮1点击之后")defclick_btn2():print("clickbutton2")defclick_......
  • Python---PyQT5之QPushButton
    fromPyQt5importQtWidgetsfromPyQt5importQtCore,QtGuiimportsysimportcv2classButtonPanel(QtWidgets.QWidget):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)select_btn=QtWidgets.QPushButton("......
  • css13 CSS Backgrounds
    https://www.w3schools.com/css/css_background.aspTheCSSbackgroundpropertiesareusedtoaddbackgroundeffectsforelements.Inthesechapters,youwilllearnaboutthefollowingCSSbackgroundproperties:background-colorbackground-imagebackgroun......
  • css12 CSS HEX Colors
    https://www.w3schools.com/css/css_colors_hex.aspAhexadecimalcolorisspecifiedwith:#RRGGBB,wheretheRR(red),GG(green)andBB(blue)hexadecimalintegersspecifythecomponentsofthecolor.HEXValueInCSS,acolorcanbespecifiedusingahex......
  • css11 CSS RGB Colors
    css11CSSRGBColorshttps://www.w3schools.com/css/css_colors_rgb.aspAnRGBcolorvaluerepresentsRED,GREEN,andBLUElightsources.RGBValueInCSS,acolorcanbespecifiedasanRGBvalue,usingthisformula:rgb(red,green,blue)Eachparameter(......
  • css10 CSS Colors
    https://www.w3schools.com/css/css_colors.asp Colorsarespecifiedusingpredefinedcolornames,orRGB,HEX,HSL,RGBA,HSLAvalues.CSSColorNamesInCSS,acolorcanbespecifiedbyusingapredefinedcolorname: <!DOCTYPEhtml><html&g......