WPF利用radiobutton制作菜单按钮

原文: WPF利用radiobutton制作菜单按钮

版权声明:欢迎转载。转载请注明出处,谢谢 https://blog.csdn.net/wzcool273509239/article/details/56480631

实现功能:鼠标移入和选中高亮显示,话不多说,直接上样式。

样式:

   <!--========================================================RadioButton 样式 通用====================================-->
    <Style x:Key="Tong_Yong_RadioButtonStyle" TargetType="{x:Type RadioButton}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <!--<Setter Property="Background" Value="#F4F4F4"/>
        <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>-->
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Margin" Value="10,10,0,0"></Setter>
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Height" Value="{Binding Height}"></Setter>
        <Setter Property="Width" Value="{Binding Width}"></Setter>
        <Setter Property="Cursor" Value="Hand"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <Grid>
                        <!--选中或者鼠标移入颜色-->
                        <Grid x:Name="B" Visibility="Collapsed" Background="{TemplateBinding BorderBrush}"/>
                        <!--默认颜色-->
                        <Grid x:Name="A" Background="{TemplateBinding Background}"/>
                        <ContentPresenter x:Name="contentPresenter" RecognizesAccessKey="True" Visibility="Collapsed"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Visibility" TargetName="A" Value="Collapsed"/>
                            <Setter Property="Visibility" TargetName="B" Value="Visible"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <!--<Setter Property="Visibility" TargetName="contentPresenter" Value="Visible"/>-->
                            <Setter Property="Visibility" TargetName="A" Value="Collapsed"/>
                            <Setter Property="Visibility" TargetName="B" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--========================================================RadioButton 样式 通用====================================-->


引用实例:

  <!--首页-->
            <RadioButton Name="rbHome" Style="{DynamicResource Tong_Yong_RadioButtonStyle}" Width="192" Height="63" IsChecked="True" Checked="RadioButton_Checked">
                <RadioButton.Background>
                    <ImageBrush ImageSource="/Agriculture.CommonUserControl;component/Images/MenuImages/home-.png"/>
                </RadioButton.Background>
                <RadioButton.BorderBrush>
                    <ImageBrush ImageSource="/Agriculture.CommonUserControl;component/Images/MenuImages/home.png"/>
                </RadioButton.BorderBrush>
                <Grid>
                    <Grid.Background>
                        <ImageBrush ImageSource="/Agriculture.CommonUserControl;component/Images/MenuImages/home.png"/>
                    </Grid.Background>
                </Grid>
            </RadioButton>




posted on 2019-03-27 09:40 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10605379.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中的RadioButton按钮的外观可以通过样式(Style)来自定义。以下是一个简单的样式示例,将RadioButton的外观更改为圆形按钮: ``` <Window.Resources> <Style x:Key="RadioButtonStyle" TargetType="{x:Type RadioButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type RadioButton}"> <Grid> <Ellipse x:Name="outerEllipse" Stroke="Black" StrokeThickness="2" Fill="White" /> <Ellipse x:Name="innerEllipse" Margin="4" Fill="Black" Opacity="0"/> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="innerEllipse" Property="Opacity" Value="1"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="outerEllipse" Property="Stroke" Value="DarkGray"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <StackPanel> <RadioButton Content="Button1" Style="{StaticResource RadioButtonStyle}" /> <RadioButton Content="Button2" Style="{StaticResource RadioButtonStyle}" /> <RadioButton Content="Button3" Style="{StaticResource RadioButtonStyle}" /> </StackPanel> ``` 在这个样式中,我们使用了两个Ellipse元素来绘制外圆和内圆,使用ContentPresenter来显示RadioButton的内容。控件模板中的触发器用于更改内圆的透明度以及鼠标悬停时的外圆颜色。 这只是一个简单的样式示例,你可以按照自己的需求来自定义RadioButton的外观。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值