简体   繁体   English

鼠标悬停时更改按钮图像

[英]Change button image on mouse hover

I have following XAML code: 我有以下XAML代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="480" Width="640" ResizeMode="NoResize">
    <Window.Background>
        <SolidColorBrush x:Name="WindowBrush" Color="#202020" />
    </Window.Background>
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="Resources/button.png"/>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <ImageBrush ImageSource="Resources/button-hover.png"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="285*"/>
            <ColumnDefinition Width="349*"/>
        </Grid.ColumnDefinitions>
        <Button Content="Button" HorizontalAlignment="Left" Height="28" Margin="21,20,0,0" VerticalAlignment="Top" Width="79" BorderThickness="1"/>
    </Grid>
</Window>

And of course, I have two images im my project — button.png and button-hover.png . 当然,我的项目中有两个图像 - button.pngbutton-hover.png But when cursor is over my button it apply default Windows style, not my button-hover.png image. 但是当光标在我的按钮上时它会应用默认的Windows样式,而不是我的button-hover.png图像。 Where is my mistake? 我的错误在哪里? Thank you. 谢谢。

This is an example style: 这是一个示例样式:

<Style TargetType="Button">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="Button">
            <Grid>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal"/>
                        <VisualState x:Name="Disabled"/>
                        <VisualState x:Name="MouseOver">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="mouseOverBackgroundImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1"/>
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Pressed">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="pressedBackgroundImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Image Name="normalBackgroundImage" Source="{TemplateBinding local:BackgroundImages.NormalBackgroundImage}"/>
                <Image Name="mouseOverBackgroundImage" Source="{TemplateBinding local:BackgroundImages.MouseOverBackgroundImage}" Opacity="0"/>
                <Image Name="pressedBackgroundImage" Source="{TemplateBinding local:BackgroundImages.PressedBackgroundImage}" Opacity="0"/>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

used in a Button with the attached properties set: 在具有附加属性集的Button中使用:

<Button local:BackgroundImages.NormalBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"
    local:BackgroundImages.MouseOverBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
    local:BackgroundImages.PressedBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"
    Content="Hello"/>

And finally the definition of those attached properties: 最后是那些附加属性的定义:

public static class BackgroundImages
{
public static readonly DependencyProperty NormalBackgroundImageProperty =
    DependencyProperty.RegisterAttached("NormalBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));

public static readonly DependencyProperty MouseOverBackgroundImageProperty =
    DependencyProperty.RegisterAttached("MouseOverBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));

public static readonly DependencyProperty PressedBackgroundImageProperty =
    DependencyProperty.RegisterAttached("PressedBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));

public static ImageSource GetNormalBackgroundImage(DependencyObject obj)
{
    return (ImageSource)obj.GetValue(NormalBackgroundImageProperty);
}

public static void SetNormalBackgroundImage(DependencyObject obj, ImageSource value)
{
    obj.SetValue(NormalBackgroundImageProperty, value);
}

public static ImageSource GetMouseOverBackgroundImage(DependencyObject obj)
{
    return (ImageSource)obj.GetValue(MouseOverBackgroundImageProperty);
}

public static void SetMouseOverBackgroundImage(DependencyObject obj, ImageSource value)
{
    obj.SetValue(MouseOverBackgroundImageProperty, value);
}

public static ImageSource GetPressedBackgroundImage(DependencyObject obj)
{
    return (ImageSource)obj.GetValue(PressedBackgroundImageProperty);
}

public static void SetPressedBackgroundImage(DependencyObject obj, ImageSource value)
{
    obj.SetValue(PressedBackgroundImageProperty, value);
}
}

For triggers, Try something like this. 对于触发器,尝试这样的事情。 I'm not sure of this: (just add the image to an /Images folder in the project) 我不确定:(只需将图像添加到项目的/ Images文件夹中)

<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="0" 
            Background="Transparent">
      <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background">
           <Setter.Value>
               <ImageBrush ImageSource="/MyProjectName;component/Images/MyImage.jpg" />
           </Setter.Value>
        </Setter>
    </Trigger>
</ControlTemplate.Triggers>

You could define attached properties for the background images for states Normal, MouseOver and Pressed (and maybe more). 您可以为状态Normal,MouseOver和Pressed(以及更多)定义背景图像的附加属性。 You would use these attached properties for the Sourceproperty of separate Image controls in a control template and modify eg the Image's Opacity when the VisualState changes. 您可以将这些附加属性用于控件模板中单独的Image控件的Sourceproperty,并在VisualState更改时修改例如Image的Opacity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM