Ismouseover does not work

Asked

Viewed 101 times

4

I have a boot in WPF and changed the style when passing the mouse on top (Ismouseover), but the problem is that not every time I move the mouse on top it changes style. When step the mouse very fast it continues as if not with the mouse on top, so do not change the style and tbm does not accept the click.

Style of Ismouseover:

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

And in the boot:

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

What could be wrong?

  • 3

    Post the whole Botaoteste code.

1 answer

1

I know this question is old but follow an example: It seems complicated but it is not, right-click on the button is select the menu Edit style > Edit a copy, it will generate this original code from the button, so in it you remove the line that contains the default over mouse 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 Controltemplate according to the link 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.