带有按钮样式XAML的RadioButton
如何在XAML中将radiobutton样式更改为按钮样式?如果选中单选按钮,如何设置背景色?带有按钮样式XAML的RadioButton,xaml,styles,radio-button,Xaml,Styles,Radio Button,如何在XAML中将radiobutton样式更改为按钮样式?如果选中单选按钮,如何设置背景色? 我想使用默认颜色(因为我使用不同的皮肤)。您需要为RadioButton定义controltemplate并在controltemplate中应用触发器。差不多 <Style x:Key="ButtonRadioButtonStyle" TargetType="{x:Type RadioButton}"> <Setter Property="Template">
我想使用默认颜色(因为我使用不同的皮肤)。您需要为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>
希望能有帮助。我经常使用这个选项
<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>
是的,这很有帮助,谢谢!但是现在,我想在我的radiobutton中添加一个图像(我想对我的两个radiobutton使用相同的样式,但两个不同的图像)。如何将图像设置为“我的样式”(在按钮上)的内容?@Ben:-为您编辑了答案。我发现单击按钮时IsChecked属性未更新。我已将按钮替换为具有以下定义的ToggleButton
,我发现它工作得更好。