WPF Control Style of Button not detecting IsMouseOver

So am trying to create button style (background opacity to black with 20% alpha and default colour of text changes to clear white) with using ResourceDictionary. I do include file into App.xaml like:

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style/ButtonStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

and to button am applying x:Key Style="{StaticResource TopBarButtons}"

so my style of it looks like (random colours just to test):

    <Style x:Key="TopBarButtons" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="HotPink"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="1" Padding="5">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="Background" Value="Lime" />
            </Trigger>
        </Style.Triggers>
    </Style>

But none of those are detecting, nothing change at all.. What’s my error?

>Solution :

Your button code is like;

<Button Style="{StaticResource TopBarButtons}" x:Name="Exit" Content="✖" Width="auto" Height="32px" Grid.Column="1" Background="Transparent" BorderBrush="Transparent" FontSize="9"/>

Remove Background="Transparent"because you have already set background in your style. If you set also background in your button, it will not apply style background.

Here is updated button;

<Button Style="{StaticResource TopBarButtons}" x:Name="Exit" Content="✖" Width="auto" Height="32px" Grid.Column="1"  BorderBrush="Transparent" FontSize="9"/>

Leave a Reply