按钮鼠标悬停以更改按钮WPF的图像

时间:2017-11-30 09:40:36

标签: c# wpf xaml

道歉,如果我错过任何事情,我是新手,也是我的代码状态(Jr开发人员正在制作中)。我正在尝试在资源字典中创建一个将执行以下操作的样式: 能够应用于所有按钮。 从白色图标png更改为蓝色图标png。 并且在理想情况下在资源字典中完成此操作,但对其他方式开放。

非常感谢任何帮助:)

这是我的app.xaml代码;

            <Style x:Key="SideMenuButton" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="{StaticResource bmBlue}" />
            <Setter Property="BorderBrush" Value="White"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontFamily" Value="Cairo"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="Padding" Value="5"/>
            <Setter Property="Margin" Value="10 20 10 0" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="5"  BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Background="{TemplateBinding Background}" >
                            <StackPanel Orientation="Horizontal">
                                <Image x:Name="image1" Visibility="Visible" MaxHeight="25" HorizontalAlignment="Center" RenderOptions.BitmapScalingMode="HighQuality" Source="C:\Users\PaulR\source\repos\eSuiteVer10\eSuiteVer10\Icons\BackIconBWblue.png"/>
                                <ContentPresenter Grid.Column="1" x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Style.Triggers>
                <Trigger Property="Button.IsMouseOver" Value="true">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="BorderBrush.Color" To="#005389" />
                                <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Background.Color" To="#fff"/>
                                <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Foreground.Color" To="#005389" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="BorderBrush.Color" To="#fff" />
                                <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Background.Color" To="#005389"/>
                                <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Foreground.Color" To="#fff" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>

                </Trigger>
            </Style.Triggers>
        </Style>

这是按钮Xaml;

   <Button Click="NewQuote_Click" Style="{StaticResource SideMenuButton}"  >
                <StackPanel Orientation="Horizontal">

                    <Image Grid.Column="0" MaxHeight="25" HorizontalAlignment="Center" RenderOptions.BitmapScalingMode="HighQuality" Source="C:\Users\PaulR\source\repos\eSuiteVer10\eSuiteVer10\Icons\NewIconBlueWhitewhite.png"/>
                    <TextBlock Text="New" Grid.Column="1" HorizontalAlignment="Center" />
                </StackPanel>
            </Button>

1 个答案:

答案 0 :(得分:0)

试试这个:

<Style x:Key="TransparentStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border>
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background">
                                     <Setter.Value>
                                      <ImageBrush ImageSource= "\Images\ButtonImages\Image.png" Stretch="Uniform"/>
                                     </Setter.Value> 
                                   </Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <Grid Background="Transparent">
                        <ContentPresenter></ContentPresenter>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>