RadioButton with Button style XAML

radio-button, styles, xaml

Solution

You need to define the controltemplate for RadioButton and apply a trigger in controltemplate. Something like.

<Style x:Key="ButtonRadioButtonStyle" TargetType="{x:Type RadioButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <Button Content="{TemplateBinding Content}"/>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasContent" Value="true">
                            <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
                            <Setter Property="Padding" Value="4,0,0,0"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And use it like.

<RadioButton Style="{DynamicResource ButtonRadioButtonStyle}">
            <Image Source="ImagePath\ABC.png"/>
        </RadioButton>

Hope it helps..

Problem

How can I change the radiobutton style to a button style in XAML? And how to set the background color if the radiobutton is checked? I would like to use the default colors (because I use differents skins).

Original source