Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    <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"/>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading