WPF利用radiobutton制作菜单按钮

1 篇文章 0 订阅

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

样式:

   <!--========================================================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>




  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值