WPF单选按钮背景色

时间:2018-08-06 15:57:40

标签: wpf

如何设置RadioButton的背景颜色?我的RadioButton在一个简单的网格中。谢谢!

<RadioButton Name="Temp" Grid.Row="1" Grid.Column="0" GroupName="SetType" Content="Temporary Sets" Margin="60,0,0,0" Checked="Radio_Checked"
                     Background="Red" Foreground="White" />

2 个答案:

答案 0 :(得分:1)

要设置Text的背景,您应该覆盖RadioButton的模板,以将Text容器的Backgroud绑定到RadioButton的Background 。只要尝试这样的事情:

<RadioButton Content="Temporary Sets" Background="Red" Foreground="White" >
    <RadioButton.Template>
        <ControlTemplate TargetType="{x:Type RadioButton}">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                <Grid>
                    <Ellipse Width="16" Height="16" Fill="{TemplateBinding Background}"
                             Stroke="{TemplateBinding BorderBrush}"
                             StrokeThickness="{TemplateBinding BorderThickness}"/>
                    <Ellipse x:Name="Checked" Width="10" Height="10" Fill="Black" Visibility="Collapsed"/>
                </Grid>
                <Label Margin="5 0 0 0" Content="{TemplateBinding Content}"
                       Foreground="{TemplateBinding Foreground}"
                       Background="{TemplateBinding Background}"/>
            </StackPanel>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="Checked" Property="Visibility" Value="Visible"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </RadioButton.Template>
</RadioButton>

答案 1 :(得分:0)

这是我完成RadioButton的方式。谢谢铁。

        <ControlTemplate x:Key="RedRadio" TargetType="{x:Type RadioButton}">
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Background="Transparent">
            <Grid>
                <Ellipse x:Name="TargetEllipse" Width="14" Height="14">
                    <Ellipse.Fill>
                        <SolidColorBrush x:Name="FillBrush" Color="#D4D4D4"/>
                    </Ellipse.Fill>
                    <Ellipse.Stroke>
                        <SolidColorBrush x:Name="StrokeBrush" Color="#434343"/>
                    </Ellipse.Stroke>
                </Ellipse>
                <Ellipse x:Name="CheckedEllipse" Width="8" Height="8" Fill="#444444" Visibility="Collapsed"/>
            </Grid>
            <Border CornerRadius="4" Margin="3 0 0 0" Padding="2 0 5 0"
                    Background="{TemplateBinding Background}">
                <Label Margin="2 0 0 0"
                   Content="{TemplateBinding Content}"
                   Foreground="{TemplateBinding Foreground}"
                   Background="{TemplateBinding Background}"/>
            </Border>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal"/>
                    <VisualState x:Name="MouseOver">
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetName="FillBrush" Storyboard.TargetProperty="Color" To="#969696" Duration="0:0:0.01"/>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </StackPanel>
        <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter TargetName="CheckedEllipse" Property="Visibility" Value="Visible"/>
                <Setter TargetName="TargetEllipse" Property="Stroke" Value="#040404"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>