billy_56
billy_56

Reputation: 679

How to set some type of highlight to a button when it's default button or when it's mouse over button?

Here is a button which I styled, but I realized that there is no way for someone who uses the application to notice when button is mouse over / selected or something like that? I just want some kind of notification when button is selected or whatever? Any kind of solution will be acceptable for me so in case button is mouse hovered, or in case button is selected to notice user that is that button!

Here is my current button:

 <Button x:Name="btnOk"
                    SnapsToDevicePixels="True"
                    UseLayoutRounding="True"
                    IsDefault="True"
                    Grid.Row="2"
                    Grid.Column="1"
                    FontSize="15"
                    Width="140"
                    BorderThickness="1"
                    HorizontalContentAlignment="Center"
                    VerticalContentAlignment="Center"
                    Foreground="#0091EA"
                    Background="White"
                    Content="Ok!"
                    BorderBrush="#0091EA" Margin="5,10,0,10" HorizontalAlignment="Left" Click="btnPotvrdi_Click">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="BlueViolet"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                </ControlTemplate>

            </Button.Template>

        </Button>

Upvotes: 2

Views: 915

Answers (1)

mm8
mm8

Reputation: 169240

Specify the default background (White) in the Style and your trigger will work as expected:

<Button x:Name="btnOk"
                    SnapsToDevicePixels="True"
                    UseLayoutRounding="True"
                    IsDefault="True"
                    Grid.Row="2"
                    Grid.Column="1"
                    FontSize="15"
                    Width="140"
                    BorderThickness="1"
                    HorizontalContentAlignment="Center"
                    VerticalContentAlignment="Center"
                    Foreground="#0091EA"
                    Content="Ok!"
                    BorderBrush="#0091EA" Margin="5,10,0,10" HorizontalAlignment="Left" Click="btnPotvrdi_Click">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="White" />
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="BlueViolet"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Background="{TemplateBinding Background}">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

A "local" value takes precedence over a value set by a Setter in a Style: https://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx

Upvotes: 1

Related Questions