C# WPF单选按钮控件模板的问题

C# WPF单选按钮控件模板的问题,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我使用下面的控制模板为我的单选按钮。我对这个代码有问题。注意,当单选按钮未选中时,复选标记不会折叠。感谢您的帮助 <ControlTemplate x:Key="RadioButtonControlTemplate" TargetType="{x:Type RadioButton}"> <BulletDecorator x:Name="bulletDecorator" SnapsToDevicePixels="True" Background="Trans

我使用下面的控制模板为我的单选按钮。我对这个代码有问题。注意,当单选按钮未选中时,复选标记不会折叠。感谢您的帮助

   <ControlTemplate x:Key="RadioButtonControlTemplate" TargetType="{x:Type RadioButton}">
        <BulletDecorator x:Name="bulletDecorator" SnapsToDevicePixels="True" Background="Transparent">
            <BulletDecorator.Bullet>
                <Grid Width="30" 
              Height="30" >
                    <Ellipse x:Name="Border"  
                Fill="{StaticResource NormalBrush}"
                StrokeThickness="3"
                Stroke="{StaticResource NormalBorderBrush}" />
                    <Ellipse x:Name="CheckMark"
                Margin="9"
                Fill="{StaticResource GlyphBrush}" />
                </Grid>
            </BulletDecorator.Bullet>
            <ContentPresenter Margin="4,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" RecognizesAccessKey="True"></ContentPresenter>
        </BulletDecorator>
        <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="False">
                <Setter Property="Visibility" TargetName="CheckMark" Value="Collapsed"/>
            </Trigger>
            <Trigger Property="HasContent" Value="True">
                <Setter Property="FocusVisualStyle">
                    <Setter.Value>
                        <Style>
                            <Setter Property="Control.Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="14,0,0,0" SnapsToDevicePixels="True"/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Setter.Value>
                </Setter>
                <Setter Property="Padding" Value="2,0,0,0"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" TargetName="bulletDecorator" Value="0.2"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

您的代码运行正常。我替换了您的资源
画笔
es,因为您没有在代码中提供它们,但是使用这个基本示例,我可以看到
单选按钮
s确实有效。。。但是,您可能需要对标签进行分类,因为它们与“复选标记”断开连接:

<ControlTemplate x:Key="RadioButtonControlTemplate" TargetType="{x:Type RadioButton}">
    <BulletDecorator x:Name="bulletDecorator" SnapsToDevicePixels="True" Background="Transparent">
        <BulletDecorator.Bullet>
            <Grid Width="30" Height="30">
                <Ellipse x:Name="Border" Fill="Red" StrokeThickness="3" Stroke="Black" />
                <Ellipse x:Name="CheckMark" StrokeThickness="1" Margin="9" Fill="Green" />
            </Grid>
        </BulletDecorator.Bullet>
        <ContentPresenter Margin="4,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" RecognizesAccessKey="True"></ContentPresenter>
    </BulletDecorator>
    <ControlTemplate.Triggers>
        <Trigger Property="IsChecked" Value="False">
            <Setter Property="Visibility" TargetName="CheckMark" Value="Collapsed"/>
            <Setter Property="Stroke" TargetName="CheckMark" Value="Blue"/>
        </Trigger>
        <Trigger Property="HasContent" Value="True">
            <Setter Property="FocusVisualStyle">
                <Setter.Value>
                    <Style>
                        <Setter Property="Control.Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="14,0,0,0" SnapsToDevicePixels="True"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="Padding" Value="2,0,0,0"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" TargetName="bulletDecorator" Value="0.2"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>


下面是它的样子(对可怕的颜色感到抱歉):


你怎么知道它实际上是未检查的?我已经试过了你的代码,它实际上工作正常。但是你至少需要2个单选按钮来测试它。因为当只使用一个单选按钮时,您可以只检查一次,然后您不能通过UI取消选中它(除非使用代码)。谢谢。我使用了两个单选按钮,但仍然不起作用。您是否尝试过使用两个单选按钮,并且效果良好?当然,请注意,您的两个单选按钮应该放在同一个容器中,否则您需要为这两个按钮设置相同的
GroupName
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <RadioButton Template="{StaticResource RadioButtonControlTemplate}" Content="Yes" />
    <RadioButton Template="{StaticResource RadioButtonControlTemplate}" Content="No" />
</StackPanel>