【问题标题】:Change style of radio buttons when selected in wpf在 wpf 中选择时更改单选按钮的样式
【发布时间】:2013-11-27 20:37:24
【问题描述】:

我有 4 个彩色单选按钮,它们的样式类似于使用

的切换按钮
BasedOn="{StaticResource {x:Type ToggleButton}}"

默认样式是选择颜色时,它将将背景颜色更改为白色。我想改变它,这样它就会在颜色周围给它一个黄色边框。任何想法如何做到这一点?

我已经尝试过这样的事情,但它似乎不会影响按钮:

<Setter Property="Template">
     <Setter.Value>
          <ControlTemplate TargetType="RadioButton">
               <ControlTemplate.Triggers>
                   <Trigger Property="IsChecked" Value="True">
                       <Setter Property="BorderBrush" Value="Yellow"/>
                       <Setter Property="BorderThickness" Value="5"></Setter>
                   </Trigger>
               </ControlTemplate.Triggers>
          </ControlTemplate>
     </Setter.Value>
</Setter>

【问题讨论】:

  • 您需要将默认的RadioButton ControlTemplate 更改为您想要的。它可能是默认设置为 #FFC5CBF9 的 SelectedBackgroundColor
  • 我试过使用 ControlTemplate 但我可能用错了?请参阅已编辑的代码问题。

标签: wpf radio-button


【解决方案1】:

你应该使用HasContent,而不是IsChecked

<Trigger Property="HasContent" Value="true">
    <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
    <Setter Property="Padding" Value="4,0,0,0"/>
</Trigger>

这是默认模板中的 sn-p,请尝试将其更改为您喜欢的模板。 我在这里有一些工作:
使用您的设置:

祝你好运

【讨论】:

  • 唯一的问题是我想改变按钮被选中时的行为方式。因此,如果他们单击该选项,我只想拥有黄色的 BorderBrush。这种方式似乎会使所有按钮都有黄色边框。
  • 你试过了吗,我当时没有运行项目,在截图之前点击了按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 2019-07-06
  • 1970-01-01
相关资源
最近更新 更多