WPF-實現按鈕的動態變化

  1. MVVM 模式基礎
    • 視圖模型(ViewModel)MainViewModel類作為視圖模型,封裝了與視圖相關的屬性和命令。它實現了INotifyPropertyChanged接口,當屬性值發生改變時,通過OnPropertyChanged方法通知視圖進行更新,確保視圖與數據的一致性。
    • 視圖(View):在 XAML 文件中定義,通過數據綁定將ButtonWidthHeight屬性與視圖模型的ButtonWidthButtonHeight屬性關聯起來,實現視圖對數據的展示。同時,將按鈕的點擊事件綁定到視圖模型的ResizeButtonCommand命令,使視圖的交互操作能觸發視圖模型中的邏輯。
  2. 動畫創建與管理
    • Storyboard 初始化:在視圖模型的InitializeStoryboard方法中,創建了一個Storyboard對象_resizeStoryboard,用于管理和協調多個動畫。
    • DoubleAnimation 創建:為按鈕的寬度和高度變化分別創建了DoubleAnimation對象。這些動畫定義了從初始值(From屬性)到目標值(To屬性)的過渡,這里目標值是初始值的 1.5 倍。動畫的持續時間由Duration屬性設置為 0.5 秒,AutoReverse屬性設置為true,使動畫在到達目標值后自動反向播放,RepeatBehavior屬性設置為RepeatBehavior.Forever,讓動畫無限循環。
  3. 命令綁定與參數傳遞
    • MyCommand 實現:定義了MyCommand類來實現ICommand接口,該類允許將一個Action<object>作為參數傳遞給構造函數,在按鈕點擊時執行相應的邏輯。
    • 命令綁定:在視圖模型的構造函數中,將ResizeButtonCommand初始化為MyCommand的實例,并關聯到ExecuteResizeButtonCommand方法,該方法處理按鈕點擊后的邏輯。
    • 參數傳遞:在視圖中,通過CommandParameter="{Binding ElementName=PART_Button}"將按鈕自身作為參數傳遞給命令。在視圖模型的ExecuteResizeButtonCommand方法中,通過判斷參數類型來獲取按鈕實例,以便正確設置動畫目標和啟動動畫。
  4. 事件處理與狀態管理
    • 點擊事件處理ExecuteResizeButtonCommand方法負責處理按鈕的點擊事件。它根據_isAnimating標志判斷當前動畫狀態,若正在動畫,則停止動畫并將按鈕大小恢復到初始值;若未動畫,則啟動動畫。每次點擊按鈕時,都會切換_isAnimating標志的值,以記錄動畫狀態。

? ? ? 5.代碼

MainWindow.xaml

<Window x:Class="WpfAppButtonResize.MainWindow"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:WpfAppButtonResize"xmlns:local2="clr-namespace:WpfAppButtonResize.ViewModel"mc:Ignorable="d"Title="MainWindow" Height="350" Width="525"><Window.DataContext><local2:MainWindowViewModel/></Window.DataContext><Grid><Button Content="點擊我" Width="{Binding ButtonWidth}" Height="{Binding ButtonHeight}" Command="{Binding ResizeButtonCommand}"CommandParameter="{Binding ElementName=PART_Button}"Name="PART_Button"/></Grid>
</Window>

MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;namespace WpfAppButtonResize.ViewModel
{public class MainWindowViewModel:Notify{private double _buttonWidth = 100;private double _buttonHeight = 50;private bool _isAnimating = false;private Storyboard _resizeStoryboard;public double ButtonWidth{get { return _buttonWidth; }set{_buttonWidth = value;OnPropertyChanged();}}public double ButtonHeight{get { return _buttonHeight; }set{_buttonHeight = value;OnPropertyChanged();}}public ICommand ResizeButtonCommand { get; private set; }public MainWindowViewModel(){ResizeButtonCommand = new MyCommand(ExecuteResizeButtonCommand);InitializeStoryboard();}private void InitializeStoryboard(){_resizeStoryboard = new Storyboard();// 寬度動畫DoubleAnimation widthAnimation = new DoubleAnimation{From = _buttonWidth,To = _buttonWidth * 1.5,Duration = TimeSpan.FromSeconds(0.5),AutoReverse = true,RepeatBehavior = RepeatBehavior.Forever};// 這里后續在設置目標時會修正_resizeStoryboard.Children.Add(widthAnimation);// 高度動畫DoubleAnimation heightAnimation = new DoubleAnimation{From = _buttonHeight,To = _buttonHeight * 1.5,Duration = TimeSpan.FromSeconds(0.5),AutoReverse = true,RepeatBehavior = RepeatBehavior.Forever};_resizeStoryboard.Children.Add(heightAnimation);}private void ExecuteResizeButtonCommand(object parameter){if (parameter is Button button){if (_isAnimating){_resizeStoryboard.Stop();ButtonWidth = 100;ButtonHeight = 50;}else{// 設置寬度動畫的目標和屬性路徑Storyboard.SetTarget(_resizeStoryboard.Children[0] as DoubleAnimation, button);Storyboard.SetTargetProperty(_resizeStoryboard.Children[0] as DoubleAnimation, new PropertyPath("(FrameworkElement.Width)"));// 設置高度動畫的目標和屬性路徑Storyboard.SetTarget(_resizeStoryboard.Children[1] as DoubleAnimation, button);Storyboard.SetTargetProperty(_resizeStoryboard.Children[1] as DoubleAnimation, new PropertyPath("(FrameworkElement.Height)"));_resizeStoryboard.Begin();}_isAnimating = !_isAnimating;}}}
}

MyCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace WpfAppButtonResize
{public class MyCommand : ICommand{private readonly Action _execute;private readonly Action<object> _execute2;private readonly Func<bool> _canExecute;public MyCommand(Action<object> execute){_execute2 = execute;}public MyCommand(Action execute, Func<bool> canExecute = null){_execute = execute;_canExecute = canExecute;}public event EventHandler CanExecuteChanged{add { CommandManager.RequerySuggested += value; }remove { CommandManager.RequerySuggested -= value; }}public bool CanExecute(object parameter){return _canExecute == null || _canExecute();}public void Execute(object parameter){if (_execute != null){_execute();}else if (_execute2 != null){_execute2(parameter);}}}
}

Notify.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;namespace WpfAppButtonResize
{public abstract class Notify : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged([CallerMemberName] string name = null){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/72746.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/72746.shtml
英文地址,請注明出處:http://en.pswp.cn/web/72746.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

主流NoSQL數據庫類型及選型分析

在數據庫領域&#xff0c;不同類型的數據庫針對不同場景設計&#xff0c;以下是四類主流NoSQL數據庫的對比分析&#xff1a; 一、核心特性對比 鍵值數據庫&#xff08;Key-Value&#xff09; 數據模型&#xff1a;簡單鍵值對存儲 特點&#xff1a;毫秒級讀寫、高并發、無固定…

西門子PLC

西門子PLC與C#通信全解析&#xff1a;從協議選型到實戰開發 一、西門子PLC通信協議概述 西門子PLC支持多種通信協議&#xff0c;需根據設備型號及項目需求選擇&#xff1a; S7協議 西門子私有協議&#xff0c;適用于S7-200/300/400/1200/1500系列PLC特點&#xff1a;直接訪問…

Visual Studio(VS)的 Release 配置中生成程序數據庫(PDB)文件

最近工作中的一個測試工具在測試多臺設備上使用過程中閃退&#xff0c;存了dump&#xff0c;但因為是release版本&#xff0c;沒有pdb&#xff0c;無法根據dump定位代碼哪塊出了問題&#xff0c;很苦惱&#xff0c;查了下怎么加pdb生成&#xff0c;記錄一下。以下是具體的設置步…

★ Linux ★ 進程(上)

Ciallo&#xff5e;(∠?ω< )⌒☆ ~ 今天&#xff0c;我將和大家一起學習 linux 進程~ ????????????????????????????? 澄嵐主頁&#xff1a;椎名澄嵐-CSDN博客 Linux專欄&#xff1a;https://blog.csdn.net/2302_80328146/category_12815302…

JAVA并發-volatile底層原理

volatile相當于是一個輕量級的synchronized&#xff0c;一般作用在變量上&#xff0c;它具有三個特性&#xff1a;可見性、有序性&#xff0c;相比于synchronized&#xff0c;他的執行成本更低。 先來說可見性&#xff0c;java存在共享變量不可見性的原因就是&#xff0c;線程…

Java面試第十一山!《SpringCloud框架》

大家好&#xff0c;我是陳一。如果文章對你有幫助&#xff0c;請留下一個寶貴的三連哦&#xff5e; 萬分感謝&#xff01; 目錄 一、Spring Cloud 是什么? 二、Spring Cloud 核心組件? 1. 服務發現 - Eureka? 2. ?負載均衡 - Ribbon? 3. 斷路器 - Hystrix? ??4. …

Transaction rolled back because it has been marked as rollback-only問題解決

transaction rolled back because it has been marked as rollback-only 簡略總結> 發生場景&#xff1a;try-catch多業務場景 發生原因&#xff1a;業務嵌套&#xff0c;事務管理混亂&#xff0c;外層業務與內層業務拋出異常節點與回滾節點不一致。 解決方式&#xff1a;修…

sql server數據遷移,springboot搭建開發環境遇到的問題及解決方案

最近搭建springboot項目開發環境&#xff0c;數據庫連的是sql server&#xff0c;遇到許多問題在此記錄一下。 1、sql server安裝教程 參考&#xff1a;https://www.bilibili.com/opus/944736210624970769 2、sql server導出、導入數據庫 參考&#xff1a;https://blog.csd…

【數學建模】灰色關聯分析模型詳解與應用

灰色關聯分析模型詳解與應用 文章目錄 灰色關聯分析模型詳解與應用引言灰色系統理論簡介灰色關聯分析基本原理灰色關聯分析計算步驟1. 確定分析序列2. 數據無量綱化處理3. 計算關聯系數4. 計算關聯度 灰色關聯分析應用實例實例&#xff1a;某企業生產效率影響因素分析 灰色關聯…

Spring配置文件-Bean實例化三種方式

無參構造方法實例化 工廠靜態方法實例化 工廠實例方法實例化

SSL 和 TLS 認證

SSL&#xff08;Secure Sockets Layer&#xff0c;安全套接層&#xff09;認證是一種用于加密網絡通信和驗證服務器身份的安全技術。它是TLS&#xff08;Transport Layer Security&#xff0c;傳輸層安全協議&#xff09;的前身&#xff0c;雖然現在大多數應用使用的是TLS&…

SpringBoot學習(三)SpringBoot整合JSP以及Themeleaf

目錄 Spring Boot 整合 JSP1. 配置依賴2. 創建WEB目錄結構&#xff0c;配置JSP解析路徑3. 創建Controller類4. 修改application.yml5. 添加jstl標簽庫的依賴6. JSP頁面7. 創建啟動類 Spring Boot 整合 Thymeleaf1. 添加Thymeleaf依賴2. Controller3. 修改application.yml配置&a…

普通鼠標的500連擊的工具來了!!!

今天介紹的這款軟件叫&#xff1a;鼠標錄制器&#xff0c;是一款大小只有54K的鼠標連點器&#xff0c;軟件是綠色單文件版。搶票&#xff0c;拍牌&#xff0c;搖號都能用上。文末有分享鏈接 在使用先我們先設置快捷鍵&#xff0c;這樣我們在錄制和停止錄制的時候會更方便。 軟件…

【MySQL】基本查詢(表的增刪查改+聚合函數)

目錄 一、Create1.1 單行數據 全列插入1.2 多行數據 指定列插入1.3 插入否則更新1.4 替換 二、Retrieve2.1 SELECT 列2.1.1 全列查詢2.1.2 指定列查詢2.1.3 查詢字段為表達式2.1.4 為查詢結果指定別名2.1.5 結果去重 2.2 WHERE 條件2.2.1 比較運算符2.2.2 邏輯運算符2.2.3 案…

JAVA中關于圖形化界面的學習(GUI)動作監聽,鼠標監聽,鍵盤監聽

動作監聽&#xff1a; 先創建一個圖形化界面&#xff0c;接著創建一個按鈕對象&#xff0c;設置按鈕的大小。 添加一個addActionListener()&#xff1b; addActionListener() 方法定義在 java.awt.event.ActionListener 接口相關的上下文中&#xff0c;許多支持用戶交互產生…

MySQL 基礎學習文檔

一、MySQL 概述 1.1 核心概念 數據庫 (DB)&#xff1a;存儲數據的結構化倉庫數據庫管理系統 (DBMS)&#xff1a;操作數據庫的軟件&#xff08;如 MySQL、Oracle&#xff09;SQL&#xff1a;操作關系型數據庫的標準語言 1.2 安裝與配置 下載地址&#xff1a;MySQL Installer…

火山引擎(豆包大模型)(抖音平臺)之火山方舟的Prompt的使用測試

前言 在大模型的使用過程當中&#xff0c;Prompt的使用非常的關鍵。原來&#xff0c;我對Prompt的理解不深&#xff0c;覺得Prompt的產生并不是很有必要。但是&#xff0c;自從使用了火山方舟中的“Prompt優解”之后&#xff0c;感受加深了&#xff0c;覺得Prompt是我們和大模型…

SpringBoot入門-(2) Spring IOC機制【附實例代碼】

SpringBoot入門-(2) Spring IOC機制 Spring Spring是一個當前主流的輕量級的框架&#xff0c;發展到形狀已經不僅僅是一個框架&#xff0c;而是形成以Spring為基礎的生態圈&#xff0c;如(Spring Boot,Spring Cloud,Spring Security等) Spring 兩大核心技術 控制反轉(IoC)面…

備賽藍橋杯之第十六屆模擬賽3期職業院校組

提示&#xff1a;本篇文章僅僅是作者自己目前在備賽藍橋杯中&#xff0c;自己學習與刷題的學習筆記&#xff0c;寫的不好&#xff0c;歡迎大家批評與建議 由于個別題目代碼量與題目量偏大&#xff0c;請大家自己去藍橋杯官網【連接高校和企業 - 藍橋云課】去尋找原題&#xff0…

【AI大模型】提示詞(Prompt)工程完全指南:從理論到產業級實踐

【AI大模型】提示詞&#xff08;Prompt&#xff09;工程完全指南&#xff1a;從理論到產業級實踐 一、Prompt 提示詞介紹&#xff1a;AI的“密碼本” 1. Prompt的底層定義與價值 本質&#xff1a;Prompt是人與AI模型的“協議語言”&#xff0c;通過文本指令激活模型的特定推理…