WFP 按钮增加图片背景,并且在按压时切换图片的记录

1、按钮控件的前台

复制代码
<UserControl x:Class="JapanSubway.UserControls.LeftMenuButton"
             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" 
             xmlns:local="clr-namespace:JapanSubway.UserControls"
             mc:Ignorable="d" 
             d:DesignHeight="50" d:DesignWidth="120" Loaded="UserControl_Loaded">

    <UserControl.Resources>
        <ControlTemplate x:Key="buttonTemplate" TargetType="Button">
            <StackPanel Orientation="Vertical">
                <Image x:Name="img" Stretch="Fill" Source="{Binding CurrentButtonImagePath}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Image>
            </StackPanel>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="img" Property="Source" Value="{Binding PressedButtonImagePath}"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Resources>

    <Grid>
        <Button IsEnabled="{Binding ButtonIsEnable}" x:Name="btn" Background="Transparent" Click="Button_Click" Template="{StaticResource buttonTemplate}"  HorizontalAlignment="Center"  VerticalAlignment="Center" BorderThickness="0"></Button>
    </Grid>
</UserControl>
复制代码

2、按钮控件的后台

复制代码
    public partial class LeftMenuButton : UserControl
    {
        public LeftMenuButtonViewModel ViewModel { get; set; }
        public int ImageIndex { get; set; }
        public LeftMenuButton()
        {
            ViewModel = new LeftMenuButtonViewModel();
            this.DataContext = ViewModel;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ViewModel.CurrentButtonImagePath = "/Images/LeftMenuButtons/left_menu_1_normal.png";
            ViewModel.PressedButtonImagePath = "/Images/LeftMenuButtons/left_menu_1_pressed.png";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.CurrentButtonImagePath = "/Images/LeftMenuButtons/left_menu_1_selected.png";
            ViewModel.ButtonIsEnable = false;
        }
    }
复制代码

3、用到的一个类

复制代码
    public class LeftMenuButtonViewModel : ViewModelBase
    {
        private string currentButtonImagePath;
        private string pressedButtonImagePath;
        private bool buttonIsEnable;
        public LeftMenuButtonViewModel()
        {
            buttonIsEnable = true;
        }
        /// <summary>
        /// 正在显示的图片的路径
        /// </summary>
        public string CurrentButtonImagePath { get => currentButtonImagePath; set { currentButtonImagePath = value; RaisePropertyChanged(); } }
        /// <summary>
        /// 按压后的图片的路径
        /// </summary>
        public string PressedButtonImagePath { get => pressedButtonImagePath; set { pressedButtonImagePath = value; RaisePropertyChanged(); } }

        public bool ButtonIsEnable { get => buttonIsEnable; set { buttonIsEnable = value; RaisePropertyChanged(); } }
    }
复制代码

4、补充用到的一个组件

  MvvmLight,用于双向绑定

posted @   星星c#  阅读(106)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示