【WPF】XAML实现按钮背景图片的点击切换

原因:要做一组搜索结果的排序按钮(类似一组RadioButton),效果像下图这样。想法是使用原生的按钮控件,将文字左对齐,整个按钮背景是一张图片,通过样式Trigger控制字体变色、背景图切换。
需求:RadioButton开关按钮,点击后切换自身按钮的背景图片。

这里写图片描述

MyRadioButton.xaml

<ResourceDictionary  x:Class="HomeDecorationPSD.Presentation.Style.MyRadioButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HomeDecorationPSD.Presentation.Style"
        mc:Ignorable="d">

    <ResourceDictionary.MergedDictionaries>
        <!-- 引入颜色字符串 -->
        <ResourceDictionary Source="/Presentation/Resources/ColorResources.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="myRadioButton" TargetType="{x:Type RadioButton}">
        <Setter Property="BorderThickness" Value="0"></Setter>
        <Setter Property="Width" Value="80"></Setter>
        <Setter Property="Height" Value="30"></Setter>
        <Setter Property="Foreground" Value="{StaticResource LightGreyColor}"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <Grid x:Name="grid" VerticalAlignment="Center">
                        <Border x:Name="border" BorderThickness="1" BorderBrush="{StaticResource LightGreyColor}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
                                HorizontalAlignment="Center" Background="#E9E9E9" Padding="5,0,0,0">
                            <ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </Grid>

                    <!-- 触发器:设置字体的颜色 -->
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="Foreground" Value="{StaticResource SoftRedColor}"/> <!-- 被引入的颜色字符串 -->
                            <Setter TargetName="border" Property="Background">
                                <Setter.Value>
                                    <ImageBrush ImageSource="/HomeDecorationPSD;component/Presentation/Resources/Images/down_arrow_selected.png" Stretch="Fill"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource LightGreyColor}"/>
                            <Setter TargetName="border" Property="Background"> <!-- 必须指明TargetName -->
                                <Setter.Value>
                                    <ImageBrush ImageSource="/HomeDecorationPSD;component/Presentation/Resources/Images/down_arrow.png" Stretch="Fill"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

坑点:

  • ControlTemplate必须使用一个Border包裹。
  • 在Trigger中给按钮设置文字颜色时,使用Foreground不需要指明TargetName,但是给背景设置图片时,必须要指明TargetName,否则无效果。(非常坑爹,运行无报错,能看到文字变色,不能看到背景图)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在WPF实现按钮的材质切换,您可以使用以下步骤: 1. 首先,确保您已经添加了Material Design的相关资源到您的项目您可以通过NuGet包管理器安装MaterialDesignThemes和MaterialDesignColors。 2. 在XAML,将按钮的样式设置为Material Design的样式。例如: ```xaml <Button Style="{StaticResource MaterialDesignRaisedButton}" Content="切换按钮" /> ``` 3. 现在,我们需要为按钮的不同状态创建样式。您可以使用VisualStateManager来定义这些样式。例如,您可以为按钮的正常状态和按下状态创建两个样式: ```xaml <Button Style="{StaticResource MaterialDesignRaisedButton}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup> <VisualState x:Name="Normal"> <Storyboard> <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Green" Duration="0:0:0.2" /> </Storyboard> </VisualState> <VisualState x:Name="Pressed"> <Storyboard> <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Red" Duration="0:0:0.2" /> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> 切换按钮 </Button> ``` 在上述示例,我们为正常状态定义了一个颜色动画,将按钮背景色设置为绿色。当按钮按下时,我们为按下状态定义了另一个颜色动画,将按钮背景色设置为红色。 通过使用VisualStateManager,您可以根据按钮的不同状态来定义不同的样式,以实现材质切换的效果。 希望能对您有所帮助!如果您有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值