按钮悬停覆盖

时间:2014-08-06 18:54:24

标签: c# xaml mouseenter

我有一个按钮,如下:

<Button HorizontalAlignment="Left" Margin="0,55,0,0" VerticalAlignment="Top" Width="350" Click="StartProcedure_Click" BorderThickness="0" Height="60" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalContentAlignment="Left" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalContentAlignment="Center" Padding="10,1,0,0" IsHitTestVisible="True">
            <StackPanel Orientation="Horizontal">
                <Image Source="resources\img_xraystarticon.png" Width="50" Height="50"/>
                <TextBlock TextElement.Foreground="#6a6869" Padding="5,0,0,0" VerticalAlignment="Center" FontSize="30" FontFamily="Century Gothic">Start Procedure</TextBlock>
            </StackPanel>
            <Button.Background>
                <ImageBrush ImageSource="resources\img_emptyrectbutton.png"/>
            </Button.Background>
        </Button>

我应用Style =&#34; {StaticResource {x:Static ToolBar.ButtonStyleKey}}&#34;样式,以删除鼠标悬停效果与按钮默认。但是,我仍然会继续遇到鼠标悬停效果,如下所示:

enter image description here

在鼠标悬停之前,以及使用鼠标悬停:

enter image description here

我试图覆盖MouseEnter事件但没有成功(我已经确认我的MouseEnter事件已被触发但我似乎无法否定默认情况下使用上面的MouseEnter设置的属性。)

我需要做些什么来避免上述情况?

谢谢!

编辑:

我已输入以下代码,但我现在无法正确执行MouseEnter函数(我希望文本更改颜色,但我没有看到它发生):

<Button Name="Button_StartMenu" HorizontalAlignment="Left" Margin="0,55,0,0" VerticalAlignment="Top" Width="350" Click="StartProcedure_Click" BorderThickness="0" Height="60" HorizontalContentAlignment="Left" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalContentAlignment="Center" Padding="10,1,0,0" IsHitTestVisible="True" MouseEnter="Button_MouseEnter">
            <Button.Background>
                <ImageBrush ImageSource="resources\img_emptyrectbutton.png"/>
            </Button.Background>
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Button.Style>
            <StackPanel Orientation="Horizontal">
                <Image Source="resources\img_xraystarticon.png" Width="50" Height="50"/>
                <TextBlock TextElement.Foreground="#6a6869" Padding="5,0,0,0" VerticalAlignment="Center" FontSize="30" FontFamily="Century Gothic"><Run Text="Start Procedure"/></TextBlock>
            </StackPanel>
        </Button>

1 个答案:

答案 0 :(得分:1)

而不是试图阻止&#39;它我将定义mouseover事件以显示与非鼠标悬停相同的事件。你可以将它们设置为相同但仍然会切换,在视觉上看起来是一样的。

How do you change Background for a Button MouseOver in WPF?

       <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Background" Value="Green"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Green"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>