【问题标题】:RadioButton whith Button style XAML带有按钮样式 XAML 的单选按钮
【发布时间】:2012-11-28 20:26:02
【问题描述】:

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

【问题讨论】:

    标签: xaml styles radio-button


    【解决方案1】:

    您需要为 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>
    

    并像使用它一样。

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

    希望对你有帮助..

    【讨论】:

    • 是的,谢谢!但是现在,我想在我的单选按钮中添加一个图像(我想对我的两个单选按钮使用相同的样式,但有两个不同的图像)。如何将图像设置为我的样式(在按钮上)的内容?
    • @Ben:- 为你编辑了答案。
    • 我发现点击按钮时IsChecked属性没有更新。我已将 Button 替换为 ToggleButton,其定义如下 &lt;ToggleButton Content="{TemplateBinding Content}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"/&gt;,我觉得效果更好。
    【解决方案2】:

    我经常使用这个选项

    <RadioButton Height="23" Width="23" ToolTip="По левому краю" GroupName="TextAlignmentGroup"
                                                    IsChecked="{Binding IsChecked}">
        <RadioButton.Template>
            <ControlTemplate TargetType="RadioButton">
                <Image Name="ImageName" Stretch="Fill"/>
                <ControlTemplate.Triggers>
                    <Trigger Property="ToggleButton.IsChecked" Value="True">
                        <Setter TargetName="ImageName" Property="Image.Source">
                            <Setter.Value>
                                <BitmapImage UriSource="../../Resources/pressed.png"></BitmapImage>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="ToggleButton.IsChecked" Value="False">
                        <Setter TargetName="ImageName" Property="Image.Source">
                            <Setter.Value>
                                <BitmapImage UriSource="../../Resources/image.png"></BitmapImage>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </RadioButton.Template>
    </RadioButton>
    

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 2016-07-11
      • 1970-01-01
      • 2019-10-18
      • 2012-01-21
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多