3
如何将单选按钮样式更改为XAML中的按钮样式?如果检查单选按钮,如何设置背景颜色? 我想使用默认颜色(因为我使用不同皮肤)。单选钮按钮样式XAML
如何将单选按钮样式更改为XAML中的按钮样式?如果检查单选按钮,如何设置背景颜色? 我想使用默认颜色(因为我使用不同皮肤)。单选钮按钮样式XAML
您需要为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>
是它的帮助谢谢!但现在,我想在我的单选按钮中添加一个图像(我想为我的两个radioButtons使用相同的样式,但是使用两个diffrents图像)。如何将图片设置为我的风格(在按钮上)的内容? – Ben
@Ben: - 为你编辑答案。 –
我发现单击按钮时IsChecked属性没有更新。我用下面的定义'ToggleButton Content =“{TemplateBinding Content}”替换了一个按钮ToggleButton IsChecked =“{Binding Path = IsChecked,Mode = TwoWay,RelativeSource = {RelativeSource TemplatedParent}}”/>',其中I找到更好的作品。 –