如何在WPF单选按钮(如复选框)中设计样式 [英] How Design a style in WPF radiobutton like checkbox

查看:109
本文介绍了如何在WPF单选按钮(如复选框)中设计样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个类似于复选框的单选按钮,所以我在Dictionary文件中定义了一种样式,例如:

I want to have a radiobutton similar to checkbox, so I define a style in Dictionary file like:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfCheckBoxLikeRadiobutton">

<Style x:Key="MyRadioButton" TargetType="{x:Type RadioButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RadioButton}">
                <Grid>
                    <CheckBox
                        IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                        Path=IsChecked, Mode=TwoWay}"
                        IsHitTestVisible="False" />
                    <CheckBox
                    IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                    Path=IsChecked, Mode=TwoWay}" Opacity="0"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

并将其合并到应用程序中。这样的xaml文件

and merge it in app.xaml file like this

<Application x:Class="WpfCheckBoxLikeRadiobutton.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfCheckBoxLikeRadiobutton"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

然后在我的窗口

<GroupBox Margin="5" Padding="5">
        <StackPanel >
            <CheckBox Content="this is checkbox" />
            <RadioButton Content="this is first radio button" Style="{DynamicResource MyRadioButton}"/>
            <RadioButton Content="this is Second radio button" Style="{DynamicResource MyRadioButton}"/>
            <RadioButton Content="this is third radio button" Style="{DynamicResource MyRadioButton}"/>
        </StackPanel>
    </GroupBox>

,但是在为单选按钮指定样式之后,该内容将消失。
我的样式有什么问题?

but after assigning style for radiobutton, that content will disappear. What is the wrong with my style?

推荐答案

发生这种情况是因为您忽略了 ControlTemplate 中控件的内容。您的样式应如下所示:

It happens because you ignore content of your control in you ControlTemplate. Your style should look like this:

<Style x:Key="MyRadioButton" TargetType="{x:Type RadioButton}">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type RadioButton}">
            <Grid>
                <CheckBox
                    IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                Path=IsChecked, Mode=TwoWay}"
                    IsHitTestVisible="False" Content="{TemplateBinding Content}" />
                <CheckBox
                IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                            Path=IsChecked, Mode=TwoWay}"   
                Content="{TemplateBinding Content}" Opacity="0"/>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

这篇关于如何在WPF单选按钮(如复选框)中设计样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆