博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF实现环(圆)形菜单
阅读量:4033 次
发布时间:2019-05-24

本文共 5948 字,大约阅读时间需要 19 分钟。

        WPF开发者QQ群: 340500857  | 微信群 -> 进入公众号主页 加入组织

 前言,接着圆形菜单。

欢迎转发、分享、点赞、在看,谢谢~。  

01

效果预览

效果预览(更多效果请下载源码体验):

02

代码如下

一、CircularMenuItemCustomControl.cs代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Media;using System.Windows.Shapes;namespace WpfCircularMenu{    [TemplatePart(Name = RotateTransformTemplateName, Type = typeof(RotateTransform))]    public class CircularMenuItemCustomControl : Control    {        private static readonly Type _typeofSelf = typeof(CircularMenuItemCustomControl);        private const string RotateTransformTemplateName = "PART_RotateTransform";        private RotateTransform _angleRotateTransform;        public double Angle        {            get { return (double)GetValue(AngleProperty); }            set { SetValue(AngleProperty, value); }        }        public static readonly DependencyProperty AngleProperty =            DependencyProperty.Register("Angle", typeof(double), typeof(CircularMenuItemCustomControl), new UIPropertyMetadata(OnAngleChanged));        private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            CircularMenuItemCustomControl control = (CircularMenuItemCustomControl)d;            control.UpdateAngle();        }        void UpdateAngle()        {            if (_angleRotateTransform == null) return;            _angleRotateTransform.Angle = Angle;        }        public string MenuTxt        {            get { return (string)GetValue(MenuTxtProperty); }            set { SetValue(MenuTxtProperty, value); }        }        public static readonly DependencyProperty MenuTxtProperty =            DependencyProperty.Register("MenuTxt", typeof(string), typeof(CircularMenuItemCustomControl), new PropertyMetadata(string.Empty));        public Brush BackgroundColor        {            get { return (Brush)GetValue(BackgroundColorProperty); }            set { SetValue(BackgroundColorProperty, value); }        }        public static readonly DependencyProperty BackgroundColorProperty =           DependencyProperty.Register("BackgroundColor", typeof(Brush), typeof(CircularMenuItemCustomControl), new PropertyMetadata(null));        public ImageSource IconImage        {            get { return (ImageSource)GetValue(IconImageProperty); }            set { SetValue(IconImageProperty, value); }        }        public static readonly DependencyProperty IconImageProperty =             DependencyProperty.Register("IconImage", typeof(ImageSource), typeof(CircularMenuItemCustomControl), new PropertyMetadata(null));               static CircularMenuItemCustomControl()        {            DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf, new FrameworkPropertyMetadata(_typeofSelf));        }        public override void OnApplyTemplate()        {            base.OnApplyTemplate();            _angleRotateTransform = GetTemplateChild(RotateTransformTemplateName) as RotateTransform;            UpdateAngle();        }           }}

二、CircularMenuItemCustomControlStyle.xaml 代码如下

三、MainWindow.xaml 代码如下

四、MainWindow.xaml.cs 代码如下

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfCircularMenu{    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window    {        public IEnumerable MenuArray        {            get { return (IEnumerable)GetValue(MenuArrayProperty); }            set { SetValue(MenuArrayProperty, value); }        }        public static readonly DependencyProperty MenuArrayProperty =            DependencyProperty.Register("MenuArray", typeof(IEnumerable), typeof(MainWindow), new PropertyMetadata(null));        public MainWindow()        {            InitializeComponent();            var menuItemModels = new List
(); var angle = 0; for (int i = 1; i <= 8; i++) { var brushConverter = new BrushConverter(); var brush = (Brush)brushConverter.ConvertFromString("#BAE766"); if (IsOdd(i)) brush = (Brush)brushConverter.ConvertFromString("#B0D440"); menuItemModels.Add(new MenuItemModel { Angle = angle, Title = $"菜单{i}", FillColor = brush, IconImage = new BitmapImage(new Uri($"pack://application:,,,/Images/{i}.png")) }); angle += 45; } MenuArray = menuItemModels; } bool IsOdd(int num) { return (num % 2) == 1; } } public class MenuItemModel { public double Angle { get; set; } public string Title { get; set; } public Brush FillColor { get; set; } public ImageSource IconImage { get; set; } }}

源码地址

github:https://github.com/yanjinhuagood/WPFDevelopers.git

gitee:https://gitee.com/yanjinhua/WPFDevelopers.git

WPF开发者QQ群: 340500857 

blogs: https://www.cnblogs.com/yanjinhua

Github:https://github.com/yanjinhuagood

出处:https://www.cnblogs.com/yanjinhua

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请著名作者 出处 https://github.com/yanjinhuagood

你可能感兴趣的文章
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
C++中使用Mongo执行count和distinct运算
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
关于AIS编码解码的两个小问题
查看>>
GitHub 万星推荐:黑客成长技术清单
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
昨夜今晨最大八卦终于坐实——人类首次直接探测到了引力波
查看>>
如何优雅、机智地和新公司谈薪水?
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>