如何在 WPF XAML 中更改单选按钮内圈的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15130363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:46:51  来源:igfitidea点击:

How to change the color of inside circle of radio button in WPF XAML

wpfradio-buttoncontroltemplate

提问by K T

This is my radio button:

这是我的单选按钮:

<RadioButton Content="Add" Grid.Column="1" Height="16" HorizontalAlignment="Left" Margin="50,13,0,0" Name="rdAdd" VerticalAlignment="Top" GroupName="AddEditRadio" Checked ="rdAdd_Checked" FontSize="12.667" />

Now the color of inner circle of radio button is orange.I want to change it to blue.

现在单选按钮内圈的颜色是橙色。我想把它改成蓝色。

Which property do I set to get "Blue" color?

我将哪个属性设置为“蓝色”?

回答by Blachshma

I'm afraid there is no direct property you can use to change the color.
What you need to do is reTemplate the RadioButton to whatever you would like.

恐怕没有可用于更改颜色的直接属性。
您需要做的是将 RadioButton 重新模板化为您想要的任何内容。

Luckily, the default templates are available, hereis the one for the RadioButton.

幸运的是,默认模板可用,这里是 RadioButton 的模板。

If I understand correctly what you want to change, you should probably modify the color of the CheckMark(or the entire BulletDecoratordepends what you're trying to achieve).

如果我正确理解您想要更改的内容,您可能应该修改CheckMark(或整个BulletDecorator取决于您要实现的目标)的颜色。

回答by D J

You have to edit the ControlTemplateof radiobutton. You can use blend and update the control template there.

您必须编辑单选按钮的ControlTemplate。您可以在那里使用混合和更新控制模板。

回答by user2896962

This is an old thread, but in case someone else is looking to change the color of the checked circle inside a radio button, this worked for me without changing the control template:

这是一个旧线程,但如果其他人希望更改单选按钮内选中圆圈的颜色,这对我有用,而无需更改控件模板:

(1) On the loaded event of the radio button, use a find visual children function (get one here) to locate the ellipse in the visual tree beneath the button.

(1) 在单选按钮的加载事件上,使用查找视觉子项功能(在此处获取一个)在按钮下方的视觉树中定位椭圆。

(2) Change the fill and/or the stroke of the ellipse found in step (1) to the desired color.

(2) 将步骤 (1) 中找到的椭圆的填充和/或笔触更改为所需的颜色。

(3) That's it!

(3) 就是这样!

回答by 00jt

<RadioButton Content="Add" Background="Blue" Foreground="Green" BorderBrush="Red" BorderThickness="5" />

<RadioButton Content="Add" Background="Blue" Foreground="Green" BorderBrush="Red" BorderThickness="5" />

(i added BorderThickness of 5 to make it easier to see)

(我添加了 5 的 BorderThickness 以使其更容易看到)

This will change the background color of the radio button to blue, the text "ADD" to green, and the border (around the button) red, like this:

这会将单选按钮的背景颜色更改为蓝色,将文本“ADD”更改为绿色,将边框(按钮周围)更改为红色,如下所示:

radio button, selected and unselected

单选按钮,选中和未选中

These are the only colors you can directly change from Radio button (without using templates and such)

这些是您可以直接从单选按钮更改的唯一颜色(不使用模板等)