P
P
postya2019-12-03 20:53:12
WPF
postya, 2019-12-03 20:53:12

How to deactivate all toggle buttons except one on click?

There are 8 toggle buttons. When one of the buttons is clicked, all the others become isEnable = False
How to do this?
It is advisable to do this in code and not in xaml, since these buttons will still call other methods

<!--Toggle Button-->
    <Style x:Key="ToggleButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="Background" Value="#535353"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="MinHeight" Value="30"/>        
        <Setter Property="Grid.Column" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderThickness="0">
                    </Border>                    
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

<ToggleButton x:Name="CategoryToggle1"
                      Grid.Row="1"
                      
                      Style="{StaticResource ToggleButton}"
                      Checked="ShowOptions"
                      Unchecked="HideOptions" />

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Space Purr, 2019-12-04
@postya

Define this style

<Window.Resources>
    <Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                        Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

Now just declare the desired number of RadioButtons
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <RadioButton Grid.Column="0" Width="50" Height="50" />
    <RadioButton Grid.Column="1" Width="50" Height="50" />
    <RadioButton Grid.Column="2" Width="50" Height="50" />
</Grid>

When you click on the button, only it becomes active. When you click on it again, it becomes inactive.
Also, if you want to make groups of such buttons, you need to add the GroupName property to the RadioButton with the name of the group.
<RadioButton GroupName="Group1"/>
<RadioButton GroupName="Group1"/>
<RadioButton GroupName="Group1"/>

<RadioButton GroupName="Group2"/>
<RadioButton GroupName="Group2"/>
<RadioButton GroupName="Group2"/>

Then pressing the button from Group1 will not affect the buttons from Group2 in any way.

A
AndrewN, 2019-12-03
@AndrewN

Just use RadioButton, this is the standard behavior for it. Set it to style

<Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}"/>

https://ru.stackoverflow.com/a/818494/218063

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question