WPF 自定义 RadioButton

<UserControl x:Class="CMApplication1.Views.CustomerRadioButtonView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"  Width="800" Height="600"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="RDOButton" TargetType="{x:Type RadioButton}">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <ControlTemplate.Resources>
                            <Storyboard x:Key="Storyboard1">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
 Storyboard.TargetName="bd">
                                    <EasingColorKeyFrame KeyTime="0" Value="White"/>
                                    <EasingColorKeyFrame KeyTime="0:0:0.3" Value="#FFAFAFAF"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="Storyboard2">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
 Storyboard.TargetName="bd">
                                    <EasingColorKeyFrame KeyTime="0" Value="#FFAFAFAF"/>
                                    <EasingColorKeyFrame KeyTime="0:0:0.5" Value="#D3D3D3"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </ControlTemplate.Resources>
                        <BulletDecorator Background="Transparent">
                            <Border x:Name="bd"  HorizontalAlignment="Center" Background="White" CornerRadius="5">
                                <Border.OpacityMask>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="Black" Offset="0"/>
                                        <GradientStop Color="White" Offset="1"/>
                                    </LinearGradientBrush>
                                </Border.OpacityMask>
                                <ContentPresenter Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </BulletDecorator>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter TargetName="bd" Property="Background" Value="gray" />
                            </Trigger>
                            <Trigger Property="IsChecked" Value="false">
                                <Setter TargetName="bd" Property="Background" Value="{x:Null}" />
                            </Trigger>
                            <Trigger Property="IsChecked" Value="true">
                                <Trigger.EnterActions>
                                    <BeginStoryboard x:Name="Storyboard2_BeginStoryboard" Storyboard="{StaticResource Storyboard2}"/>
                                </Trigger.EnterActions>
                                <Setter TargetName="bd" Property="Background" Value="#FF606060" />
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
                                </MultiTrigger.EnterActions>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                    <Condition Property="IsChecked" Value="False"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="bd" Property="Background" Value="Red" />
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal" Focusable="True">
            <StackPanel.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF727272" Offset="1" />
                    <GradientStop Color="#FFFFFF" Offset="0.654" />
                    <GradientStop Color="#FEFEFE" Offset="0" />
                    <GradientStop Color="#FFFFFFFB" Offset="0.994" />
                </LinearGradientBrush>
            </StackPanel.Background>
            <RadioButton Width="138" Height="128" Margin="2,5" Cursor="Hand" Name="yingyetingToolBarButton" VerticalAlignment="Top"
  Style="{DynamicResource RDOButton}" SnapsToDevicePixels="True">
                <StackPanel HorizontalAlignment="Center">
                    <Image Width="128" Height="128" Margin="0,5" Source="/images/printer.png"/>
                    <TextBlock><Run Text="营业厅"/></TextBlock>
                </StackPanel>
            </RadioButton>
            <RadioButton Width="138" Height="128" Margin="2,5" Cursor="Hand" Name="yingyechaxunToolBarButton" VerticalAlignment="Top"
  Style="{DynamicResource RDOButton}" SnapsToDevicePixels="True">
                <StackPanel HorizontalAlignment="Center">
                    <Image Width="128" Height="128"  Margin="0,5" Source="/images/itunes.png"/>
                    <TextBlock><Run Text="营业厅"/></TextBlock>
                </StackPanel>
            </RadioButton>
            <RadioButton Width="138" Height="128" Margin="2,5" Cursor="Hand" Name="jiaojiebanToolBarButton" VerticalAlignment="Top"
  Style="{DynamicResource RDOButton}" SnapsToDevicePixels="True">
                <StackPanel HorizontalAlignment="Center">
                    <Image Width="128" Height="128" Margin="0,5" Source="/images/safari.png"/>
                    <TextBlock><Run Text="营业厅"/></TextBlock>
                </StackPanel>
            </RadioButton>
        </StackPanel>
               
    </Grid>
</UserControl>


知识共享许可协议
《WPF 自定义 RadioButton》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     155.77ms