如何使用触发器或任何其他事件更改onmoveover上的按钮颜色,onmouseleave in wpf

ibr*_*han 5 c# wpf triggers button

我用一些按钮开发了一个WPF应用程序.现在我想通过使用触发器或任何其他事件来改变onmouseover,onmouseleave,onmouseenter上的那些按钮的颜色.任何建议PLZ提前谢谢.

Ste*_*nan 5

在所需的事件中,您可以像这样设置背景颜色......

// Change the background color of button1 to Blue
button1.Background = Brushes.Blue;
Run Code Online (Sandbox Code Playgroud)

您也可以在触发器中设置它:

<!-- Button will change from Blue to Yellow on MouseOver -->
<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Blue" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Yellow" />
        </Trigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

如需详情,请查看属性触发器的部分这一文章.

  • 这个解决方案的问题可以在这个问题中找到:http://stackoverflow.com/questions/1302756/why-is-the-buttons-background-changing同样的问题发生在我身上 - 它似乎没有设置背景. (2认同)