How to get a group of toggle buttons to act like radio buttons in WPF?

wpf radio button group selected value
wpf radiobutton group
wpf radio button toggle button style
wpf togglebutton
wpf radio button binding
wpf radiobutton ischecked=(binding)
wpf radio button default checked
radio button in wpf mvvm

I have a group of buttons that should act like toggle buttons, but also as radio buttons where only one button can be selected / pressed down at a current time. It also need to have a state where none of the buttons are selected / pressed down.

The behavior will be kind of like Photoshop toolbar, where zero or one of the tools are selected at any time!

Any idea how this can be implemented in WPF?

The easiest way is to style a ListBox to use ToggleButtons for its ItemTemplate

<Style TargetType="{x:Type ListBox}">
    <Setter Property="ListBox.ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <ToggleButton Content="{Binding}" 
                              IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then you can use the SelectionMode property of the ListBox to handle SingleSelect vs MultiSelect.

WPF Behavior to Ensure a ToggleButton/CheckBox is Selected , How to get a group of toggle buttons to act like radio buttons in WPF? Hey, I've actually developed a solution for this. You can override the control template for  I have a group of buttons that should act like toggle buttons, but also as radio buttons where only one button can be selected / pressed down at a current time. It also need to have a state where none of the buttons are selected / pressed down.

This is easiest way in my opinion.

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

Enjoy! -- Pricksaw

Show radiobuttons as togglebuttons · Issue #320 · fluentribbon , WPF Behavior to Ensure a ToggleButton/CheckBox is Selected only one CheckBox in a group was selected, so they would act like a RadioButton group, The Unchecked event handler will check get the attached behavior  We will need to use a converter parameter to specify which value a radio button corresponds to. If your property equals this value then this button will be checked and other buttons unchecked. This solution works regardless whether you set the GroupName property or not, though you should normally set it.

<RadioButton Content="Point" >
    <RadioButton.Template>
        <ControlTemplate>
            <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                          Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
        </ControlTemplate>
    </RadioButton.Template>
</RadioButton>

it works for me, enjoy!

The RadioButton control, The toggle button look is catchy and is easy to make a group of toggle buttons act like a group of radio buttons; by the way it would be even  This works as expected with normal radio buttons. However, with the toggle button styled buttons it no longer shows one of the buttons as being selected. In the following demo if you repeatedly click the 'Toggle enabled' button you can see that the selected button is still highlighted when it is re-enabled.

you could always use a generic event on the Click of the ToggleButton that sets all ToggleButton.IsChecked in a groupcontrol(Grid, WrapPanel, ...) to false with the help of the VisualTreeHelper; then re-check the sender. Or something in the likes of that.

private void ToggleButton_Click(object sender, RoutedEventArgs e)
    {
        int childAmount = VisualTreeHelper.GetChildrenCount((sender as ToggleButton).Parent);

        ToggleButton tb;
        for (int i = 0; i < childAmount; i++)
        {
            tb = null;
            tb = VisualTreeHelper.GetChild((sender as ToggleButton).Parent, i) as ToggleButton;

            if (tb != null)
                tb.IsChecked = false;
        }

        (sender as ToggleButton).IsChecked = true;
    }

Working with WPF RadioButton using C# and XAML, The RadioButton control allows you to give your user a list of possible options and you will see that, as promised, only one RadioButton can be checked at the same time. But what if you want several groups of radio buttons, each with their own, control and display it, but this is just a shortcut to make things easier for you. The (Ribbon)ToggleButton's behavior is not the same as a radio button's group where you can set a group and only one can be checked at a time. You'll need to write up a RibbonToggleButton.Checked event to handle the desired behavior. There's nothing wrong with this method (considering mvvm) since the code-behind is specifically dealing with UI behavior.

you can put grid with radiobuttons in it, and create button like template for raduiobuttons. than just programmaticaly remove check if you don't want buttons to be toggled

T225951, The RadioButton class in WPF represents a Radio Button control. A RadioButton is usually used in a group with multiple options where one must be selected. The RadioButton looks like the following. replace the "if..else" statement with the "switch" statement to make your code read and work better. Radio buttons and switch button don't do the same thing. Radio buttons let you choose only one option in a set, and a switch lets you turn an option on or off. As an alternative to Radio button in your case you could use a toggle button with exclusive selection.

RadioButton Class (System.Windows.Controls), I need radio buttons in a group look like toggle buttons. how can I make a ToggleButton to be untoggled only if any other button in a group  A toogle is a Check box, not a Radio button. You are using the wrong tool. You can change the box to a circle and it will look the same as the radio button, but provides the functionality you have requested. Radio buttons are simply for selecting one item from a group of items by checking the button.

Buttons that act like radio buttons - MSDN, Represents a button that can be selected, but not cleared, by a user. The RadioButton is a control that is usually used as an item in a group of RadioButton controls. You can find a list of visual properties in the Changing the Visual Structure of a Control section in the Gets or sets whether the ToggleButton is checked. When you pushed one in, the previosly selected poped out. So only one station can be selected at a time. The RadioButton control has the same behavior. It lets the user choose one option out of a few. It the list of options gets longer, you should prefer a combo or list box instead.

Let's code! Data binding to a Radio Button , The key is to make RadioButton use the ToggleButton as its control template, that's all. Ouch Liu , Developer ≈ Designer. Edited by Ouch Liu  The RadioButton class in WPF represents a Radio Button control. The code example in this tutorial shows how to create and use a RadioButton control available in WPF and XAML.

Comments
  • thank you for sharing! i was wondering whether this is good practice... but it seems to be ok..
  • @LeeLouviere: Would you care to elaborate why not? Isn't that a prime example of how to use the item template?
  • It's a bad example because you confuse display and behavior. You change the display via datatemplate but you still have the behavior of a listbox and not the behavior of a set of radio/toggle buttons. Keyboard interaction is weird. A better solution would be an ItemsControl with RadioButtons, styled as ToggleButtons (see the other highest voted answer).
  • Why reinvent the wheel?
  • This one should be selected answer by far. Gives you all the benefits of a radiobutton without the drawbacks of binding to some controller. I first tried binding to separate invisible radiobutton set, but that had unnecessary overhead and ended up with a bug where all the buttons clicked looked highlighted but not necessarily checked.
  • Or, if you need to apply this to all RadioButtons inside an element (e.g. a Grid), use <Grid.Resources> <Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}" /> </Grid.Resources>.
  • one disadvantage to this method is that you cannot uncheck a radio button by clicking on a checked one.
  • Can be custom style used on such togglebutton somehow?
  • @wondra You can only assign a single style to a FrameworkElement. However, you can adapt romkyns solution and inherit from the ToggleButton style: <Style BasedOn="{StaticResource {x:Type ToggleButton}}" x:Key="Blubb" TargetType="RadioButton"><Setter Property="Background" Value="Yellow" /></Style>
  • I know this isn't the question. But what to do if the button should behave like the radiobuttons where one must always be selected?