Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set button content on button click trigger in xaml

I would like to set button content using trigger in XAML without writing code behind:

Every time button is clicked, content should be changed: Something like button clicked first time, button content = "Hi", Second time clicked, button content = "Bye", Third time clicked, button content = "Hi" again.

    <Button x:Name="btn" Content="Hi">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference btn}"
                                           Storyboard.TargetProperty="Content">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0"
                                        Value="Bye"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
                <StopStoryboard>
                </StopStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>

So it works for setting button content once to Bye, but how to set it back to Hi on the next click ??

I am also not sure if this is the most efficient way of setting the trigger, if you have better solution, feel free.

like image 822
inside Avatar asked Sep 13 '13 21:09

inside


1 Answers

You want a button with two state, which seem exactly what a ToggleButton is. Here is a style changing the Content like you want:

<Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Button.IsDefaulted" Value="True">
                                <Setter Property="BorderBrush" TargetName="border" Value="Red"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="border" Value="#FFBEE6FD"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Content" TargetName="contentPresenter" Value="Bye"/>

                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                                <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
like image 98
Benoit Catherinet Avatar answered Oct 18 '22 19:10

Benoit Catherinet