wpf 自定义单选按钮 RadioButton

新建RadioButtonEx.cs

public class RadioButtonEx : RadioButton
    {

        public Geometry SelectIcon
        {
            get { return (Geometry)GetValue(SelectIconProperty); }
            set { SetValue(SelectIconProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectIcon.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectIconProperty =
            DependencyProperty.Register("SelectIcon", typeof(Geometry), typeof(RadioButtonEx), new PropertyMetadata(default(Geometry)));




        public Brush IconColor
        {
            get { return (Brush)GetValue(IconColorProperty); }
            set { SetValue(IconColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconColorProperty =
            DependencyProperty.Register("IconColor", typeof(Brush), typeof(RadioButtonEx), new PropertyMetadata(Brushes.Red));



        /// <summary>
        /// 圆角
        /// </summary>
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CornerRadius.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(RadioButtonEx), new PropertyMetadata(new CornerRadius(2)));



        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(RadioButtonEx), new PropertyMetadata(""));



    }

新建RadioButtonEx资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ex="clr-namespace:HL.SelfTicket.Controls">
    <PathGeometry x:Key="rdbSelect">M431.47 793.782c-11.365 0-22.332-4.378-30.589-12.286l-235.495-225.535c-17.64-16.894-18.245-44.891-1.35-62.528 16.894-17.64 44.891-18.245 62.532-1.351l201.055 192.552 364.692-443.171c15.519-18.86 43.39-21.567 62.253-6.049 18.861 15.519 21.568 43.39 6.048 62.251l-394.992 479.993c-7.821 9.504-19.248 15.319-31.534 16.047-0.874 0.052-1.748 0.078-2.621 0.078z</PathGeometry>

    <Style TargetType="{x:Type ex:RadioButtonEx}">
        <Setter Property="Height" Value="30"/>
        <Setter Property="IconColor" Value="#f14253"/>
        <Setter Property="FontSize" Value="26"/>
        <Setter Property="Foreground" Value="#333"/>
        <Setter Property="SelectIcon" Value="{StaticResource rdbSelect}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="#979797"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ex:RadioButtonEx}">
                    <Grid x:Name="grid" VerticalAlignment="Center">
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                            <Border Height="{TemplateBinding Height}" 
                                        Width="{TemplateBinding Height}" 
                                        CornerRadius="{TemplateBinding CornerRadius}"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}">
                                <Path Visibility="Collapsed" x:Name="select" Data="{StaticResource rdbSelect}" Margin="5" Fill="{TemplateBinding IconColor}" Stretch="Fill"></Path>
                            </Border>
                            <TextBlock Text="{TemplateBinding Text}" VerticalAlignment="Center" Margin="10,0,0,0"
                                        Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}"/>
                        </StackPanel>
                    </Grid>
                    <!--触发器:设置选中状态符号-->
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="Visibility" Value="Visible" TargetName="select"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

 

 

原文:https://www.cnblogs.com/zisai/p/11050978.html 

posted @ 2019-06-19 14:12  姚赛  阅读(2301)  评论(0编辑  收藏  举报