How do I change the background of my button on mou

2019-07-31 22:04发布

问题:

I have a WPF application written in C#.

I have made a button. I changed the background color.

How can I change the bluish color when mouse is over my button? And how to control animation/change time?

回答1:

http://msdn.microsoft.com/en-us/magazine/cc163421.aspx

You will need to look into creating a 'style' for your button. This will be done in XAML, you will find Microsoft Expression Blend really simplifies the design of most aspects of your WPF projects.



回答2:

    <Style x:Key="StyleButton" TargetType="{x:Type Button}">
    <Setter Property="FontFamily" Value="Tahoma"></Setter>
            <Setter Property="Height" Value="50"></Setter>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Foreground" Value="Blue"></Setter>
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush>
                        <LinearGradientBrush.GradientStops>
                            <GradientStop Offset="0" Color="White"></GradientStop>
                            <GradientStop Offset="1" Color="Lime"></GradientStop>
                            <GradientStop Offset="2" Color="Yellow"></GradientStop>
                        </LinearGradientBrush.GradientStops>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsFocused" Value="True"/>
                        <Condition Property="IsMouseOver" Value="True"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Foreground" Value="Yellow"/>
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush>
                                <LinearGradientBrush.GradientStops>
                                    <GradientStop Color="blue" Offset="0"/>
                                    <GradientStop Color="#CC00FF" Offset="1"/>
                                    <GradientStop Color="#FF00CC" Offset="2"/>

                                </LinearGradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </MultiTrigger>
            </Style.Triggers>
        </Style>