在WPF上的鼠标上更改按钮图像

问题描述:

为什么不改变鼠标上的按钮图像,而是改变背面颜色?

Why isn't it changing the button's image on mouse ever , instead it's changing the back color ??

      <Button x:Name="button" Content="" ClickMode="Press" BorderThickness="0" UseLayoutRounding="True" HorizontalAlignment="Left" Margin="401,10,0,0" VerticalAlignment="Top" Width="26" Height="28" BorderBrush="{x:Null}" Grid.Column="1" Foreground="{x:Null}">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background">
                        <Setter.Value>
                            <ImageBrush ImageSource="pack://application:,,,/OffPo Diagnostic Tool;component/resources/c_ml.bmp" Stretch="Fill"/>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background">
                                <Setter.Value>
                                    
                                    <ImageBrush ImageSource="pack://application:,,,/OffPo Diagnostic Tool;component/resources/c_mo.bmp"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
            </Button>




Style.Triggers将由Button默认模板触发器覆盖。

The Style.Triggers would be covered by Button default Template Triggers .

如果你想要 鼠标悬停时更改按钮图像。

If you want  to change button image when mouse over.

您需要覆盖Button模板,试试这个:

You need override the Button template, try this:

    <Button
            x:Name="button"
            Grid.Column="1"
            Width="66"
            Height="68"
            Margin="0,10,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            BorderBrush="{x:Null}"
            BorderThickness="0"
            ClickMode="Press"
            Content=""
            Foreground="{x:Null}"
            UseLayoutRounding="True">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="Button">
                                <Border
                                    x:Name="border"
                                    Background="Red"
                                    BorderBrush="Blue"
                                    BorderThickness="3"
                                    CornerRadius="5"
                                    TextBlock.Foreground="White">
                                    <Grid>
                                        <Image
                                            x:Name="buttonImage"
                                            Source="http://sipi.usc.edu/database/misc/4.1.02.tiff"
                                            Stretch="UniformToFill" />
                                        <ContentPresenter
                                            Margin="{TemplateBinding Padding}"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center" />
                                    </Grid>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="true">
                                        <Setter TargetName="border" Property="Background" Value="DarkRed" />
                                        <Setter TargetName="border" Property="BorderBrush" Value="DarkRed" />
                                        <Setter TargetName="buttonImage" Property="Source" Value="http://sipi.usc.edu/database/misc/4.1.01.tiff" />

                                    </Trigger>
                                    <Trigger Property="IsPressed" Value="true">
                                        <Setter TargetName="border" Property="Background" Value="blue" />
                                        <Setter TargetName="border" Property="BorderBrush" Value="Red" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Button.Style>
        </Button>

此致,

Bob