当前位置: 首页 > news >正文

WPF实现一个带旋转动画的菜单栏

WPF实现一个带旋转动画的菜单栏

  • 一、创建WPF项目及文件
    • 1、创建项目
    • 2、创建文件夹及文件
    • 3、添加引用
  • 二、代码实现
    • 2.ControlAttachProperty类

一、创建WPF项目及文件

1、创建项目

打开VS2022,创建一个WPF项目,如下所示
在这里插入图片描述在这里插入图片描述

2、创建文件夹及文件

创建资源文件夹,添加字体图标文件,添加 Menu样式文件,如下所示
在这里插入图片描述
创建公共附加属性用控件类库,并创建对应的 ControlAttachProperty 类文件如下:
在这里插入图片描述
在这里插入图片描述

3、添加引用

右键 Menu_WPF 添加引用,将类库引用到当前项目
在这里插入图片描述

二、代码实现

2.ControlAttachProperty类

代码如下(示例):

using Microsoft.Win32;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using CheckBox = System.Windows.Controls.CheckBox;
using ComboBox = System.Windows.Controls.ComboBox;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
using RadioButton = System.Windows.Controls.RadioButton;
using RichTextBox = System.Windows.Controls.RichTextBox;
using TextBox = System.Windows.Controls.TextBox;namespace Common.UserControl.Extession
{/// <summary>/// 公共附加属性/// </summary>public static class ControlAttachProperty{#region FocusBorderBrush 焦点边框色,输入控件public static readonly DependencyProperty FocusBorderBrushProperty = DependencyProperty.RegisterAttached("FocusBorderBrush", typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));public static void SetFocusBorderBrush(DependencyObject element, Brush value){element.SetValue(FocusBorderBrushProperty, value);}public static Brush GetFocusBorderBrush(DependencyObject element){return (Brush)element.GetValue(FocusBorderBrushProperty);}#endregion#region MouseOverBorderBrush 鼠标进入边框色,输入控件public static readonly DependencyProperty MouseOverBorderBrushProperty =DependencyProperty.RegisterAttached("MouseOverBorderBrush", typeof(Brush), typeof(ControlAttachProperty),new FrameworkPropertyMetadata(Brushes.Transparent,FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits));/// <summary>/// Sets the brush used to draw the mouse over brush./// </summary>public static void SetMouseOverBorderBrush(DependencyObject obj, Brush value){obj.SetValue(MouseOverBorderBrushProperty, value);}/// <summary>/// Gets the brush used to draw the mouse over brush./// </summary>[AttachedPropertyBrowsableForType(typeof(TextBox))][AttachedPropertyBrowsableForType(typeof(CheckBox))][AttachedPropertyBrowsableForType(typeof(RadioButton))][AttachedPropertyBrowsableForType(typeof(DatePicker))][AttachedPropertyBrowsableForType(typeof(ComboBox))][AttachedPropertyBrowsableForType(typeof(RichTextBox))]public static Brush GetMouseOverBorderBrush(DependencyObject obj){return (Brush)obj.GetValue(MouseOverBorderBrushProperty);}#endregion#region AttachContentProperty 附加组件模板/// <summary>/// 附加组件模板/// </summary>public static readonly DependencyProperty AttachContentProperty = DependencyProperty.RegisterAttached("AttachContent", typeof(ControlTemplate), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));public static ControlTemplate GetAttachContent(DependencyObject d){return (ControlTemplate)d.GetValue(AttachContentProperty);}public static void SetAttachContent(DependencyObject obj, ControlTemplate value){obj.SetValue(AttachContentProperty, value);}#endregion#region WatermarkProperty 水印/// <summary>/// 水印/// </summary>public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(""));public static string GetWatermark(DependencyObject d){return (string)d.GetValue(WatermarkProperty);}public static void SetWatermark(DependencyObject obj, string value){obj.SetValue(WatermarkProperty, value);}#endregion#region FIconProperty 字体图标/// <summary>/// 字体图标/// </summary>public static readonly DependencyProperty FIconProperty = DependencyProperty.RegisterAttached("FIcon", typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(""));public static string GetFIcon(DependencyObject d){return (string)d.GetValue(FIconProperty);}public static void SetFIcon(DependencyObject obj, string value){obj.SetValue(FIconProperty, value);}#endregion#region FIconSizeProperty 字体图标大小/// <summary>/// 字体图标/// </summary>public static readonly DependencyProperty FIconSizeProperty = DependencyProperty.RegisterAttached("FIconSize", typeof(double), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(12D));public static double GetFIconSize(DependencyObject d){return (double)d.GetValue(FIconSizeProperty);}public static void SetFIconSize(DependencyObject obj, double value){obj.SetValue(FIconSizeProperty, value);}#endregion#region FIconMarginProperty 字体图标边距/// <summary>/// 字体图标/// </summary>public static readonly DependencyProperty FIconMarginProperty = DependencyProperty.RegisterAttached("FIconMargin", typeof(Thickness), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));public static Thickness GetFIconMargin(DependencyObject d){return (Thickness)d.GetValue(FIconMarginProperty);}public static void SetFIconMargin(DependencyObject obj, Thickness value){obj.SetValue(FIconMarginProperty, value);}#endregion#region AllowsAnimationProperty 启用旋转动画/// <summary>/// 启用旋转动画/// </summary>public static readonly DependencyProperty AllowsAnimationProperty = DependencyProperty.RegisterAttached("AllowsAnimation", typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, AllowsAnimationChanged));public static bool GetAllowsAnimation(DependencyObject d){return (bool)d.GetValue(AllowsAnimationProperty);}public static void SetAllowsAnimation(DependencyObject obj, bool value){obj.SetValue(AllowsAnimationProperty, value);}/// <summary>/// 旋转动画刻度/// </summary>private static DoubleAnimation RotateAnimation = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(200)));/// <summary>/// 绑定动画事件/// </summary>private static void AllowsAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){var uc = d as FrameworkElement;if (uc == null) return;if (uc.RenderTransformOrigin == new Point(0, 0)){uc.RenderTransformOrigin = new Point(0.5, 0.5);RotateTransform trans = new RotateTransform(0);uc.RenderTransform = trans;}var value = (bool)e.NewValue;if (value){RotateAnimation.To = 180;uc.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, RotateAnimation);}else{RotateAnimation.To = 0;uc.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, RotateAnimation);}}#endregion#region CornerRadiusProperty Border圆角/// <summary>/// Border圆角/// </summary>public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));public static CornerRadius GetCornerRadius(DependencyObject d){return (CornerRadius)d.GetValue(CornerRadiusProperty);}public static void SetCornerRadius(DependencyObject obj, CornerRadius value){obj.SetValue(CornerRadiusProperty, value);}#endregion#region LabelProperty TextBox的头部Label/// <summary>/// TextBox的头部Label/// </summary>public static readonly DependencyProperty LabelProperty = DependencyProperty.RegisterAttached("Label", typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));[AttachedPropertyBrowsableForType(typeof(TextBox))]public static string GetLabel(DependencyObject d){return (string)d.GetValue(LabelProperty);}public static void SetLabel(DependencyObject obj, string value){obj.SetValue(LabelProperty, value);}#endregion#region LabelTemplateProperty TextBox的头部Label模板/// <summary>/// TextBox的头部Label模板/// </summary>public static readonly DependencyProperty LabelTemplateProperty = DependencyProperty.RegisterAttached("LabelTemplate", typeof(ControlTemplate), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));[AttachedPropertyBrowsableForType(typeof(TextBox))]public static ControlTemplate GetLabelTemplate(DependencyObject d){return (ControlTemplate)d.GetValue(LabelTemplateProperty);}public static void SetLabelTemplate(DependencyObject obj, ControlTemplate value){obj.SetValue(LabelTemplateProperty, value);}#endregion}
}

Menu样式文件实现

    <!--背景透明的HeaderItem样式,带旋转动画--><Style x:Key="TransparentHeaderMenuItem" TargetType="{x:Type MenuItem}"><Setter Property="BorderBrush" Value="{StaticResource MenuBorderBrush}"/><Setter Property="BorderThickness" Value="1"/><Setter Property="Background" Value="{StaticResource MenuBackground}"/><Setter Property="Foreground" Value="{StaticResource TextForeground}"/><Setter Property="FontSize" Value="{StaticResource FontSize}"/><Setter Property="Margin" Value="2,0,2,0"/><Setter Property="Height" Value="30"/><Setter Property="local:ControlAttachProperty.FIconSize" Value="18"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type MenuItem}"><Grid x:Name="Bg" VerticalAlignment="{TemplateBinding VerticalAlignment}"><StackPanel Orientation="Horizontal" x:Name="border" VerticalAlignment="Center" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"><!--icon--><TextBlock x:Name="PART_Icon" Text="{TemplateBinding Icon}" Foreground="{TemplateBinding Foreground}" local:ControlAttachProperty.AllowsAnimation="{Binding IsSubmenuOpen,RelativeSource={RelativeSource TemplatedParent}}"FontSize="{TemplateBinding local:ControlAttachProperty.FIconSize}" Style="{StaticResource FIcon}" /><TextBlock x:Name="txtHeader" Margin="5 0 0 0" FontSize="{TemplateBinding FontSize}" HorizontalAlignment="Stretch" Text="{TemplateBinding Header}" VerticalAlignment="Center" Grid.Column="1" Foreground="{TemplateBinding Foreground}"/></StackPanel><!--弹出子集菜单容器--><Popup x:Name="SubMenuPopup" AllowsTransparency="true" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom"  Focusable="false" VerticalOffset="0"PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"><Border Background="{TemplateBinding Background}"  CornerRadius="0" Margin="5" Effect="{StaticResource DefaultDropShadow}"BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"><Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True"><StackPanel Margin="10" IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/></Grid></Border></Popup></Grid><!--触发器--><ControlTemplate.Triggers><!--高亮状态--><Trigger Property="IsHighlighted" Value="true"><Setter Property="Foreground" Value="{StaticResource MouseOverForeground}"></Setter></Trigger><Trigger Property="IsPressed" Value="true"><Setter Property="Foreground" Value="{StaticResource PressedForeground}"></Setter></Trigger><Trigger Property="IsEnabled" Value="False"><Setter TargetName="border" Value="{StaticResource DisableOpacity}" Property="Opacity"></Setter></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>

MainWindow样式实现

  <Menu Width="80" Height="30" Margin="3" Background="Transparent" ><MenuItem Header="展开菜单" Style="{StaticResource TransparentHeaderMenuItem}" Padding="0" Icon="&#xe61b;" ><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe605;" Header="设置" /><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe656;" Header="插件管理" /><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe65e;"  Header="用户管理" /><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe659;" Header="修改密码" /><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe7c6;" Header="在线更新" /><Separator Background="SpringGreen" Style="{StaticResource HorizontalSeparatorStyle}"/><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe6d5;" Header="问题反馈" /><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe65f;" Header="技术支持" /><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe600;" Header="帮助" /><MenuItem Style="{StaticResource TransparentHeaderMenuItem}" Icon="&#xe622;" Header="关于" /></MenuItem></Menu>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • js | 原型链
  • 慢查询sql索引优化
  • Docker:基础概念、架构与网络模式详解
  • Linux先行一步
  • java通过pdf-box插件完成对pdf文件中图片/文字的替换
  • 鸿蒙Navigation路由能力汇总
  • 自动化测试高级控件交互方法:TouchAction、触屏操作、点按,双击,滑动,手势解锁!
  • 编程的法则 迪米特法则(Law of Demeter)也称为“最少知识原则(Principle of Least Knowledge)包括如何实践
  • 微服务之间Feign调用
  • mac M1 创建Mysql8.0容器
  • C# Blazor Server与JS互操作无法获取大数据量返回值
  • 2024-07-13 Unity AI状态机2 —— 项目介绍
  • Dify中的高质量索引模式实现过程
  • 华为USG6000V防火墙NAT智能选举
  • Python和C++行人轨迹预推算和空间机器人多传感融合双图算法模型
  • 【Linux系统编程】快速查找errno错误码信息
  • codis proxy处理流程
  • ES6系统学习----从Apollo Client看解构赋值
  • gulp 教程
  • IDEA常用插件整理
  • JS函数式编程 数组部分风格 ES6版
  • JS学习笔记——闭包
  • Sass Day-01
  • Selenium实战教程系列(二)---元素定位
  • spring-boot List转Page
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • Yii源码解读-服务定位器(Service Locator)
  • 闭包,sync使用细节
  • 微信小程序--------语音识别(前端自己也能玩)
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 用 Swift 编写面向协议的视图
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • 自定义函数
  • # 利刃出鞘_Tomcat 核心原理解析(二)
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #我与Java虚拟机的故事#连载04:一本让自己没面子的书
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (30)数组元素和与数字和的绝对差
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (python)数据结构---字典
  • (第一天)包装对象、作用域、创建对象
  • (独孤九剑)--文件系统
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (十八)三元表达式和列表解析
  • (算法)大数的进制转换
  • (小白学Java)Java简介和基本配置
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • ./configure,make,make install的作用
  • .Net core 6.0 升8.0