原创

WPF ImageRadioButton

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/q136780964/article/details/79261647

增加选中背景,透明度,和透明度模式。样例:
这里写图片描述
这里写图片描述

 public class ImageRadioButton : RadioButton
    {
        static ImageRadioButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageRadioButton), new FrameworkPropertyMetadata(typeof(ImageRadioButton)));
        }



        public Brush CheckedBackground
        {
            get { return (Brush)GetValue(CheckedBackgroundProperty); }
            set { SetValue(CheckedBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CheckedBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckedBackgroundProperty =
            DependencyProperty.Register("CheckedBackground", typeof(Brush), typeof(ImageRadioButton));

        public bool IsOpacityMode
        {
            get { return (bool)GetValue(IsOpacityModeProperty); }
            set { SetValue(IsOpacityModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsOpacityMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsOpacityModeProperty =
            DependencyProperty.Register("IsOpacityMode", typeof(bool), typeof(ImageRadioButton), new PropertyMetadata(false));



        public double MouseOverOpacity
        {
            get { return (double)GetValue(MouseOverOpacityProperty); }
            set { SetValue(MouseOverOpacityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MouseOverOpacity.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MouseOverOpacityProperty =
            DependencyProperty.Register("MouseOverOpacity", typeof(double), typeof(ImageRadioButton), new PropertyMetadata(0.8));

    }

style:

 <Style TargetType="{x:Type local:ImageRadioButton}">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ImageRadioButton}">
                    <Grid x:Name="grid" Background="{TemplateBinding Background}"/>
                    <ControlTemplate.Triggers>
                        <!--透明度模式-->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsOpacityMode" Value="True"/>
                                <Condition Property="IsMouseOver" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Opacity" Value="1"/>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsOpacityMode" Value="True"/>
                                <Condition Property="IsPressed" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Opacity" Value="1"/>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsOpacityMode" Value="True"/>
                                <Condition Property="IsChecked" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Opacity" Value="1"/>
                        </MultiTrigger>

                        <!--背景图片模式-->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsOpacityMode" Value="False"/>
                                <Condition Property="IsMouseOver" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Opacity" Value="{Binding MouseOverOpacity,RelativeSource={RelativeSource Mode=Self}}"/>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsOpacityMode" Value="False"/>
                                <Condition Property="IsPressed" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Opacity" Value="{Binding MouseOverOpacity,RelativeSource={RelativeSource Mode=Self}}"/>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsOpacityMode" Value="False"/>
                                <Condition Property="IsChecked" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="grid" Value="{Binding CheckedBackground,RelativeSource={RelativeSource TemplatedParent}}"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
展开阅读全文

没有更多推荐了,返回首页

博客模板©2019 由CSDN提供 模版主题为:skin-yellow by CSDN官方博客 设计