我试图改变RadioButton
的内容的不透明度,这是一些文本可能是TextBlock
。问题是这个当前的样式它改变文本的不透明度就好了,但它没有显示实际的单选按钮本身。请帮忙。
<UserControl.Resources>
<Style x:Key="RadioLabel" TargetType="{x:Type RadioButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<TextBlock x:Name="RadioLabel"
Text="{TemplateBinding Content}"
Background="{x:Null}" />
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="RadioLabel"
Property="Background"
Value="Transparent"/>
<Setter TargetName="RadioLabel"
Property="Opacity"
Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
这是我的RadioButton
<RadioButton x:Name="rbn1" Style="{StaticResource RadioLabel}" GroupName="rbnApplication"
IsChecked="{Binding Path=CurrentApplicationType,
Converter={StaticResource EnumComparisonConverter},
ConverterParameter={x:Static utils:ApplicationType.QXManagerRegulatory}}"
IsEnabled="{Binding Path=CurrentApplicationType,
Converter={StaticResource EnumComparisonConverter},
ConverterParameter={x:Static utils:ApplicationType.QXManagerRegulatory}}"
Content="QX Manager Regulatory"/>
这是因为你将ControlTemplate
设置为你的RadioButton
。这会改变你的整体控制。相反,你需要使用ContentTemplate
来改变你的Content
。
下面Style
应该适用于您的情况。
<Style x:Key="RadioLabel" TargetType="{x:Type RadioButton}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type RadioButton}">
<TextBlock x:Name="RadioLabel"
Text="{TemplateBinding Content}"
Background="{x:Null}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsChecked,
RelativeSource={RelativeSource Mode=TemplatedParent}}"
Value="False">
<Setter TargetName="RadioLabel"
Property="Background"
Value="Transparent"/>
<Setter TargetName="RadioLabel"
Property="Opacity"
Value="0.5"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
一般来说,最简单的方法是声明一个RadioButton(或任何其他控件)然后在属性选项卡中转到Miscellaneous - > Template,左键单击右边的小方块按钮并选择“Convert to local”值”。这将扩展完整的现有ControlTemplate,然后您可以编辑和/或添加触发器等:
<RadioButton x:Name="template" >
<RadioButton.Template>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border x:Name="radioButtonBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="100" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1,1,2,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid x:Name="markGrid" Margin="2">
<Ellipse x:Name="optionMark" Fill="#FF212121" MinWidth="6" MinHeight="6" Opacity="0"/>
</Grid>
</Border>
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="True">
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="14,0,0,0" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Padding" Value="4,-1,0,0"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="radioButtonBorder" Value="#FFF3F9FF"/>
<Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="#FF5593FF"/>
<Setter Property="Fill" TargetName="optionMark" Value="#FF212121"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" TargetName="radioButtonBorder" Value="#FFE6E6E6"/>
<Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="#FFBCBCBC"/>
<Setter Property="Fill" TargetName="optionMark" Value="#FF707070"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="radioButtonBorder" Value="#FFD9ECFF"/>
<Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="#FF3C77DD"/>
<Setter Property="Fill" TargetName="optionMark" Value="#FF212121"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Opacity" TargetName="optionMark" Value="1"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter Property="Opacity" TargetName="optionMark" Value="0.56"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</RadioButton.Template>
</RadioButton>