F
F
Floydreme2018-04-13 18:22:41
WPF
Floydreme, 2018-04-13 18:22:41

How to change button color after clicking?

There is a task: to make it so that when you click on the button, its background changes. Made the following code:

<Style x:Key="BackColor"  TargetType="{x:Type Button}">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontSize" Value="50"/>
            <Setter Property="Content" Value="SEND"/>
            <Setter Property="Background" Value="#FF06C548"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontFamily" Value="Global User Interface"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border CornerRadius="17" Background="{TemplateBinding Background}">
                            <ContentPresenter
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"  
                                Height="63"
                                Margin="192,17,177,10"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#FF0EAC44"/>
                </Trigger>
                <Trigger Property ="ClickMode" Value="Release">
                    <Setter Property="Background" Value="#FF1B74CB"/>
                </Trigger>
            </Style.Triggers> 
        </Style>

But when I launch the application, the button is not pressed at all, and the background has become necessary. Tried through "IsPressed", but it didn't fit. I would be very grateful for help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Floydreme, 2018-04-15
@FloydReme

figured it out myself

<Style x:Key="BackColor"  TargetType="{x:Type Button}">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontSize" Value="50"/>
            <Setter Property="Content" Value="SEND"/> 
            <Setter Property="Background" Value="#FF06C548"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontFamily" Value="Global User Interface"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border CornerRadius="17" Background="{TemplateBinding Background}">
                                <ContentPresenter
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center"  
                                Height="63"
                                Margin="192,17,177,10"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
                                            To="#FF0EAC44"
                                            Duration="0:0:0.4"
                                            FillBehavior="HoldEnd"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
                                            To="#FF06C548"
                                            Duration="0:0:0.4"
                                            FillBehavior="HoldEnd"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
                                            To="#FF1B74CB"
                                            Duration="0:0:0.4"
                                            FillBehavior="HoldEnd"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question