C# WPF 按钮鼠标悬停图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12271916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:42:47  来源:igfitidea点击:

WPF Button Mouseover Image

c#wpfxamlwpf-controls

提问by Ishikawa

I am learning C# and XAML to build windows applications. I wanted to create a button that has an image as its background. But when hovering over the button, the background of the button should change to another "highlighted" image. I attempted to add the background images into Resources.resx. I had to create a custom button using xaml styles to get rid of the default highlight effect of a wpf button.

我正在学习 C# 和 XAML 来构建 Windows 应用程序。我想创建一个以图像为背景的按钮。但是当鼠标悬停在按钮上时,按钮的背景应该变成另一个“突出显示”的图像。我试图将背景图像添加到 Resources.resx 中。我不得不使用 xaml 样式创建一个自定义按钮来摆脱 wpf 按钮的默认突出显示效果。

I created a custom button from some code I found on SO. The code is (in a new resource dictionary):

我从在 SO 上找到的一些代码创建了一个自定义按钮。代码是(在一个新的资源字典中):

    <!-- This style is used for buttons, to remove the WPF default 'animated' mouse over effect -->
    <Style x:Key="StartMenuButtons" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border" 
                        BorderThickness="0" 
                        Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">


                        <!-- UPDATE THE BUTTON BACKGROUND -->
                        <Setter Property="Background" Value="WHAT GOES HERE"  TargetName="border"/>


                    </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

What do I put so that the background changes to another image, whether it is in my resources.resx or another location? (Not sure where to put the image to access it). I searched SO but the solutions I found were not exactly what I am dealing with. If this is a duplicate question, I apologize.

我应该放什么才能让背景变成另一个图像,无论是在我的 resources.resx 还是其他位置?(不确定将图像放在哪里才能访问它)。我搜索了 SO,但我找到的解决方案并不是我正在处理的。如果这是一个重复的问题,我深表歉意。

Summary:

概括:

How do I change the background image of a button on a mouse over trigger in XAML? Where do I put the image so that it can be accessed in the trigger code?

如何更改 XAML 中鼠标悬停触发器上按钮的背景图像?我将图像放在哪里,以便可以在触发代码中访问它?

UpdateThis is what I have put as the trigger action, but the image does not update. I made sure to set the image build action to resource and put it in a folder called Resources.

更新这是我作为触发器操作的内容,但图像没有更新。我确保将图像构建操作设置为 resource 并将其放在名为Resources的文件夹中。

The code is:

代码是:

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background">
          <Setter.Value>
             <ImageBrush ImageSource="/Simon;component/Resources/btn_bg_hover.jpg" />
          </Setter.Value>
        </Setter>
     </Trigger>

The file structure is

文件结构是

Simon
    Simon
        Resources
            all the images
        Fonts
        bin
        obj
        Properties

Solution

解决方案

The following is the complete code to allow for a mouseover image change on the button:

以下是允许在按钮上更改鼠标悬停图像的完整代码:

<!-- This style is used for buttons, to remove the WPF default 'animated' mouse over effect -->
    <Style x:Key="StartMenuButtons" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border" 
                        BorderThickness="0" 
                        Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" TargetName="border">
                            <Setter.Value>
                                <ImageBrush ImageSource="Resources/btn_bg_hover.jpg" />
                            </Setter.Value>
                        </Setter>

                    </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

For the actual image, I placed it in the Resourcesfolder that is in the root directory. After importing the images in there using the resources tool in visual studio, I updated the image build settingsto Resourcein the Properties pane.

对于实际图像,我将其放在根目录下的Resources文件夹中。使用 Visual Studio 中的资源工具将图像导入其中后,我在“属性”窗格中将图像构建设置更新为“资源”。

Thanks for the solution dbaseman

感谢 dbaseman 的解决方案

采纳答案by McGarnagle

I think it's easier to just add the image to an /Imagesfolder in the project. Then this is the syntax you use:

我认为将图像添加到/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>
</ControlTemplate>

(Assuming your image MyImage.jpgis in the Imagesfolder in the root of your project.)

(假设您的图像MyImage.jpg位于Images项目根目录的文件夹中。)

Just make sure that MyImage.jpghas its "Build Action" set to "Resource".

只需确保将MyImage.jpg其“构建操作”设置为“资源”。

回答by Yannick Turbang

Here is another way to do this.

这是执行此操作的另一种方法。

You can use the two events of the image MouseEnter and MouseLeave. Now in your code do this.

您可以使用图像 MouseEnter 和 MouseLeave 两个事件。现在在您的代码中执行此操作。

XAML

XAML

<Image x:Name="image1" HorizontalAlignment="Left" Height="142" Margin="64,51,0,0" VerticalAlignment="Top" Width="150" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave"   />

C#

C#

private void image1_MouseEnter(object sender, MouseEventArgs e)
{
    string packUri = @"pack://application:,,,/Resources/Polaroid.png";
    image1.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;
}

private void image1_MouseLeave(object sender, MouseEventArgs e)
{
    string packUri = @"pack://application:,,,/Resources/PolaroidOver.png";
    image1.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;
}