Search code examples
c#wpfxamlbuttontogglebutton

Uncheck ToggleButton when other button is clicked


As per description, I have a ToggleButton and a Button.

The Button appears when the ToggleButton is toggled, and disappears when untoggled.

I would like to trigger the ToggleButton property IsChecked and set it to false when the Button is clicked. I know that it should be done with triggers but I can't figure out how.

<ToggleButton x:Name="EditBtn" VerticalAlignment="Top" Focusable="False">

<Button VerticalAlignment="Bottom" Height="24" Width="24" 
        Visibility="{Binding ElementName=EditBtn, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"
        Command="{Binding ElementName=InfoList, Path=DataContext.SaveCommand}" CommandParameter="{Binding}" >
    <Button.Style>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonTransparentStyle}">
            <Style.Triggers>
                <!-- Something here? -->
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

Solution

  • Here is an option using StoryBoard:

    <Button VerticalAlignment="Bottom" Height="24" Width="24" 
        Visibility="{Binding ElementName=EditBtn, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"
        Command="{Binding ElementName=InfoList, Path=DataContext.SaveCommand}" CommandParameter="{Binding}" >
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <Storyboard>
                        <BooleanAnimationUsingKeyFrames
                            Storyboard.TargetName="EditBtn"
                            Storyboard.TargetProperty="(ToggleButton.IsChecked)">
                            <DiscreteBooleanKeyFrame
                                KeyTime="00:00:00"
                                Value="False"/>
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>
    

    You can also bind the IsChecked property of your ToggleButton, and just modify the attached property in your ViewModel (in the void triggered by your Button's Command).