您当前的位置: 首页 > 

寒冰屋

暂无认证

  • 1浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

WPF实现Win10汉堡菜单

寒冰屋 发布时间:2021-10-24 22:32:06 ,浏览量:1

有小伙伴提出需要实现Win10汉堡菜单效果。                           

图片

     由于在WPF中没有现成的类似UWP的汉堡菜单,所以我们自己实现一个。

01

代码如下

一、创建 Win10Menu.cs 菜单继承 ContentControl 代码如下。

图片

Win10Menu.cs实现思路如下

1、IsOpen :判定是否展开、收起 。

2、Content :存放菜单集合。

3、SelectionIndicatorColor :选中菜单状态栏颜色。                 

4、MenuItemForeground:菜单集字体颜色。

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WPFDevelopers.Controls
{
    public class Win10Menu : ContentControl
    {
        public new List Content
        {
            get { return (List)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public new static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(List), typeof(Win10Menu),new FrameworkPropertyMetadata(null));

        static Win10Menu()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Win10Menu), new FrameworkPropertyMetadata(typeof(Win10Menu)));
        }

        public override void BeginInit()
        {
            Content = new List();
            base.BeginInit();
        }

        public bool IsOpen
        {
            get { return (bool)GetValue(IsOpenProperty); }
            set
            {
                SetValue(IsOpenProperty, value);
            }
        }

        public static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register("IsOpen", typeof(bool), typeof(Win10Menu), new PropertyMetadata(true));


        public System.Windows.Media.Brush MenuIconColor
        {
            get { return (System.Windows.Media.Brush)GetValue(MenuIconColorProperty); }
            set { SetValue(MenuIconColorProperty, value); }
        }

        public static readonly DependencyProperty MenuIconColorProperty =
            DependencyProperty.Register("MenuIconColor", typeof(System.Windows.Media.Brush), typeof(Win10Menu), new PropertyMetadata(Brushes.White));


        public Brush SelectionIndicatorColor
        {
            get { return (Brush)GetValue(SelectionIndicatorColorProperty); }
            set { SetValue(SelectionIndicatorColorProperty, value); }
        }

        public static readonly DependencyProperty SelectionIndicatorColorProperty =
            DependencyProperty.Register("SelectionIndicatorColor", typeof(Brush), typeof(Win10Menu), new PropertyMetadata(Brushes.Red));

        public Brush MenuItemForeground
        {
            get { return (Brush)GetValue(MenuItemForegroundProperty); }
            set { SetValue(MenuItemForegroundProperty, value); }
        }

        public static readonly DependencyProperty MenuItemForegroundProperty =
            DependencyProperty.Register("MenuItemForeground", typeof(Brush), typeof(Win10Menu), new PropertyMetadata(Brushes.Transparent));
    }
}

二、创建 Win10Menu.xaml 为 Win10Menu.cs 进行布局 代码如下 

图片

Win10Menu.xaml 思路如下

1、ToggleButton :控制IsChecked动画如下

IsOpen = False:DoubleAnimation修改controls:Win10Menu的Width为 180。

IsOpen = True :DoubleAnimation修改controls:Win10Menu的Width为 50。

2、ListBox:ItemsSource="{TemplateBinding Content}"展示菜单集合。          

需注意如下 

ScrollViewer.HorizontalScrollBarVisibility="Disabled",

不然当Win10Menu的Width 为50时会出现滚动条。



        
        
            
                
                    
                        
                    
                
            
        
        
            
                
                
                
                    
                        
                            
                                
                            
                        
                    
                
            
        

    
        
        
        
            
                
                    
                        
                    
                
            
        


        
        
        
        
            
                
                    
                        
                            
                        
                        
                    
                
            
        
        
            
                
                    
                        
                            
                        
                    
                
                
                    
                        
                            
                        
                    
                
            
        

三、创建 Win10MenuItem.cs 继承 ListBoxItem 代码如下。

图片

Win10MenuItem.cs 思路如下

1、Text 菜单文本内容展示。

2、Icon 菜单图标为ImageSource类型。

3、SelectionIndicatorColor选中的侧边状态颜色。

4、SelectionCommand 选中事件。


using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace WPFDevelopers.Controls
{
    public class Win10MenuItem : ListBoxItem
    {
        static Win10MenuItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Win10MenuItem), new FrameworkPropertyMetadata(typeof(Win10MenuItem)));
        }
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Win10MenuItem), new PropertyMetadata(string.Empty));


        public ImageSource Icon
        {
            get { return (ImageSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(ImageSource), typeof(Win10MenuItem), new PropertyMetadata(null));

        public Brush SelectionIndicatorColor
        {
            get { return (Brush)GetValue(SelectionIndicatorColorProperty); }
            set { SetValue(SelectionIndicatorColorProperty, value); }
        }

        public static readonly DependencyProperty SelectionIndicatorColorProperty =
            DependencyProperty.Register("SelectionIndicatorColor", typeof(Brush), typeof(Win10MenuItem), new PropertyMetadata(Brushes.Blue));

        public ICommand SelectionCommand
        {
            get { return (ICommand)GetValue(SelectionCommandProperty); }
            set { SetValue(SelectionCommandProperty, value); }
        }

        public static readonly DependencyProperty SelectionCommandProperty =
            DependencyProperty.Register("SelectionCommand", typeof(ICommand), typeof(Win10MenuItem), new PropertyMetadata(null));
    }
}

四、创建 Win10MenuItem.xaml 为 Win10MenuItem.cs 进行布局 代码如下 


        
            
                
                    
                        
                    
                
            
        


    

    
    
    
    
    

    

    
    
    
    


    
        
        
        
        
        
        
        
            
                
                    
                        
                    
                    
                        
                            
                        
                        
                            
                        
                        
                            
                        
                        
                            
                            
                        
                        
                            
                            
                            
                        
                    
                
            
        

    
        
        
        
        
            
                
                    
                        
                            
                                
                                
                            
                            
                                
                                    
                                        
                                        
                                    
                                    
                                    
                                
                            
                            
                        
                    
                    
                        
                            
                        
                        
                            
                                
                                    
                                        
                                            
                                        
                                    
                                
                            
                        
                    
                
            
        

五、创建Win10MenuExample.xaml代码如下

图片

Win10MenuExample.xaml实现思路如下

1、Grid布局分为两列设置第零列Width为Auto自适应。

2、第零列为Win10menu。

3、第一列为Frame设置NavigationUIVisibility="Hidden"


    
        
            
            
        
        
            
                
                
                
                
                
            
        
        
    

六、创建Win10MenuExample.xaml.cs代码如下

图片

Win10MenuExample.xaml实现思路如下

1、定义List赋值集合为菜单需要跳转的页面。

2、构造为myFrame.Navigate(_uriList[0]);第零页。

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFDevelopers.Samples.Helpers;

namespace WPFDevelopers.Samples.ExampleViews.Win10Menu
{
    /// 
    /// Win10MenuExample.xaml 的交互逻辑
    /// 
    public partial class Win10MenuExample : UserControl
    {
        private List _uriList = new List()
        {
            new Uri("ExampleViews/Win10Menu/HomePage.xaml",UriKind.Relative),
            new Uri("ExampleViews/Win10Menu/EdgePage.xaml",UriKind.Relative),
        };
        public Win10MenuExample()
        {
            InitializeComponent();
            myFrame.Navigate(_uriList[0]);
        }

        public ICommand HomeCommand => new RelayCommand(obj =>
        {
            myFrame.Navigate(_uriList[0]);
        });
        public ICommand EdgeCommand => new RelayCommand(obj =>
        {
            myFrame.Navigate(_uriList[1]);
        });
        public ICommand CloudCommand => new RelayCommand(obj =>
        {
            MessageBox.Show("点击了云盘");
        });
        public ICommand MailCommand => new RelayCommand(obj =>
        {
            MessageBox.Show("点击了邮件");
        });
        public ICommand VideoCommand => new RelayCommand(obj =>
        {
            MessageBox.Show("点击了视频");
        });
    }
}

02

效果预览

视频地址

源码地址如下

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

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

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

Github:https://github.com/yanjinhuagood

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

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.0512s