Search code examples
c#radio-buttonwinui-3togglebutton

How to Style WinUi 3 RadioButtons as a group of toggle buttons?


I need to style some grouped radio buttons as toggle buttons like below, how to implement this in WinUi

Here is the question to do the same in WPF. Style WPF radio button as toggle button with correct IsEnabled behaviour

enter image description here

  <StackPanel >
      <RadioButtons Header="Options:" MaxColumns="3">
          <RadioButton Content="Option 1" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option1RadioButton"/>
          <RadioButton Content="Option 2" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option2RadioButton"/>
          <RadioButton Content="Option 3" Checked="RadioButton_Checked" AutomationProperties.AutomationId="Option3RadioButton"/>
      </RadioButtons>
  </StackPanel> 

Solution

  • This should work:

    <RadioButtons>
        <RadioButtons.Resources>
            <Style TargetType="RadioButton">
                <Style.Setters>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="RadioButton">
                                <ToggleButton
                                    Content="{x:Bind Content, Mode=OneWay}"
                                    IsChecked="{x:Bind IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style.Setters>
            </Style>
        </RadioButtons.Resources>
        <RadioButton
            AutomationProperties.AutomationId="Option1RadioButton"
            Checked="RadioButton_Checked"
            Content="Option 1" />
        <RadioButton
            AutomationProperties.AutomationId="Option2RadioButton"
            Checked="RadioButton_Checked"
            Content="Option 2" />
        <RadioButton
            AutomationProperties.AutomationId="Option3RadioButton"
            Checked="RadioButton_Checked"
            Content="Option 3" />
    </RadioButtons>