K
K
kokapuk2021-07-17 22:17:48
WPF
kokapuk, 2021-07-17 22:17:48

How to change color when hovering over a control?

I tried that, but it doesn't work.

<Window.Resources>
        <ControlTemplate TargetType="Button" x:Key="btTemp">
            <Border x:Name="ButtonBorders" Background="{TemplateBinding Background}" BorderThickness="0" CornerRadius="5" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
                <ContentControl Foreground="{TemplateBinding Foreground}" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="ButtonBorders" Property="Background" Value="LightSeaGreen"/>
                    <Setter Property="Control.Foreground" Value="DarkOliveGreen"/>
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    <Grid Background="#FF6A6A6A">
        <Button Template="{StaticResource btTemp}" Content="Button" HorizontalAlignment="Left" Margin="268,214,0,0" VerticalAlignment="Top" Width="174" Height="67" FontSize="22" FontFamily="Segoe UI Black" Background="#FF785A5A" Foreground="White"/>

    </Grid>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Bereznikov, 2021-07-20
@kokapuk

This is how it works:

<ControlTemplate x:Key="TestButtonTemplate" TargetType="Button">
    <Border Background="{TemplateBinding Background}"
            BorderThickness="0"
            CornerRadius="5"
            Height="{TemplateBinding Height}"
            Width="{TemplateBinding Width}">
        <ContentControl Foreground="{TemplateBinding Foreground}"
                        Content="{TemplateBinding Content}"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        FontSize="{TemplateBinding FontSize}"
                        FontFamily="{TemplateBinding FontFamily}"/>
    </Border>

    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

It's lazy to figure out the reasons, but you probably need to change the properties of the control itself, since you bind to them in the Border

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question