RadioButton连按钮样式XAML

Ben*_*Ben 4 xaml styles radio-button

如何在XAML中将radiobutton样式更改为按钮样式?如果检查单选按钮,如何设置背景颜色?我想使用默认颜色(因为我使用不同的皮肤).

D J*_*D J 6

您需要为RadioButton定义controltemplate并在controltemplate中应用触发器.就像是.

<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>
Run Code Online (Sandbox Code Playgroud)

并使用它.

<RadioButton Style="{DynamicResource ButtonRadioButtonStyle}">
            <Image Source="ImagePath\ABC.png"/>
        </RadioButton>
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你..

  • 我发现单击按钮时 IsChecked 属性没有更新。我已经用具有以下定义的 ToggleButton 替换了按钮`&lt;ToggleButton Content="{TemplateBinding Content}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"/&gt;`,我发现效果更好。 (2认同)