WPF RadioButton控件模板的问题

时间:2014-10-22 23:11:06

标签: c# wpf wpf-controls

我正在使用控制模板下面的单选按钮。我对此代码有疑问。未选中单选按钮时,注意 CheckMark 不会折叠。任何帮助表示赞赏。

   <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>

1 个答案:

答案 0 :(得分:1)

您的代码运行正常。我替换了您的资源Brush es,因为您没有在代码中提供它们,但是使用这个基本示例,我可以看到RadioButton确实有用...您可能需要整理虽然标签,因为它们与'复选标记'断开连接:

<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>

...

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <RadioButton Template="{StaticResource RadioButtonControlTemplate}" Content="Yes" />
    <RadioButton Template="{StaticResource RadioButtonControlTemplate}" Content="No" />
</StackPanel>

这就是它的样子(抱歉可怕的颜色):

enter image description here