IsMouseOver does not work

4

I have a button in WPF and I changed the style by hovering the mouse (IsMouseOver), but the problem is that every time I move the mouse over it changes its style. When the mouse step very fast it continues as if it were not with the mouse on top, thus not changing the style and tbm does not accept the click.

IsMouseOver style:

<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
    <Setter Property="Background">
        <Setter.Value>
            <ImageBrush ImageSource="\Img\on.png" />
        </Setter.Value>
    </Setter>
</Trigger>

And in the button:

<Button Style="{StaticResource BotaoTeste}" Grid.Row="0" Grid.Column="0" Click="Clicado">Passe o Mouse</Button>

What could be wrong?

    
asked by anonymous 17.10.2014 / 21:10

1 answer

1

I know this question is old but it follows an example: It looks complicated but it is not, right click on the button is select the edit style > edit a copy, it will generate this original button code, then in it you remove the line that contains the mouse over the pattern that is commented in the code. RenderMouseOver="{TemplateBinding IsMouseOver}

Now add your rule along with the other standard Triggers as shown in the example below.

You can also use this other solution by editing the ControlTemplate as shown in the example here !:

<Style x:Key="ButtonFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" 
                               SnapsToDevicePixels="true" 
                               Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" 
                               StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#F3F3F3" Offset="0"/>
        <GradientStop Color="#EBEBEB" Offset="0.5"/>
        <GradientStop Color="#DDDDDD" Offset="0.5"/>
        <GradientStop Color="#CDCDCD" Offset="1"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" 
                                         Background="{TemplateBinding Background}"                                            
                                         RenderPressed="{TemplateBinding IsPressed}" 
                                         RenderDefaulted="{TemplateBinding IsDefaulted}" 
                                         SnapsToDevicePixels="true">

                                        <!--RenderMouseOver="{TemplateBinding IsMouseOver}" -->

                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                          Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" 
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <ImageBrush ImageSource="\Img\on.png"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
    
05.03.2015 / 18:09