C# WPF抽屉效果实现(C# WPF Material Design UI: Navigation Drawer & PopUp Menu)

时间如流水,只能流去不流回!点赞再看,养成习惯,这是您给我创作的动力!本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform、WPF、ASP.NET Core等,亦有C++桌面相关的Qt Quick和Qt Widgets等,只分享自己熟悉的、自己会的。

二、本文背景

有网友给站长Dotnet9留言:“WPF中能否实现UWP中SplitView效果,即抽屉效果吗?” Dotnet9记得国外开源C# WPF控件库MaterialDesignInXaml中有这种效果,可以查看本站写的推荐文章:MaterialDesignInXaml控件介绍,站长也是个喜欢码砖的人,对代码是很感兴趣的,遂Google了一个YouTube视频敲出本文实现的代码,希望对他和您有用。

三、代码实现

站长使用.Net Core 3.1创建的WPF工程,创建PopUpAndNav解决方案后,需要添加两个Nuget库:MaterialDesignThemes和MaterialDesignColors。

添加Material两个库

工程比较简单,主要就是演示窗口MainWindow:

解决方案结构

代码不多,我就全部贴上代码吧。

添加MaterialDesignInXaml样式:App.xaml

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:local="clr-namespace:PopUpAndNav"

            StartupUri="MainWindow.xaml">

   

       

           

               

               

               

               

           

       

   

演示窗口MainWindow.xaml代码,使用简单的自定义窗口,看效果图,有右上角的标题栏菜单及左上角的抽屉菜单:

        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:PopUpAndNav"

        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"

        mc:Ignorable="d" Foreground="White"

        Title="MainWindow" Height="600" Width="1080" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">

   

       

           

               

               

           

       

       

           

               

               

           

       

   

   

       

           

       

       

           

       

   


   

       

           

           

               

               

                   

                       

                   

               

               

                   

                       

                           

                           

                       

                   

                   

                       

                           

                           

                       

                   

                   

                       

                           

                           

                       

                   

                   

                       

                           

                           

                       

                   

                   

                       

                           

                           

                       

                   

               

           

       

   

后台MainWindow.xaml.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.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 PopUpAndNav

{

    ///

    /// Interaction logic for MainWindow.xaml

    ///

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void ButtonPopUpLogout_Click(object sender, RoutedEventArgs e)

        {

            Application.Current.Shutdown();

        }

        private void ButtonOpenMenu_Click(object sender, RoutedEventArgs e)

        {

            ButtonOpenMenu.Visibility = Visibility.Collapsed;

            ButtonCloseMenu.Visibility = Visibility.Visible;

        }

        private void ButtonCloseMenu_Click(object sender, RoutedEventArgs e)

        {

            ButtonOpenMenu.Visibility = Visibility.Visible;

            ButtonCloseMenu.Visibility = Visibility.Collapsed;

        }


        private void GridTitle_MouseDown(object sender, MouseButtonEventArgs e)

        {

            DragMove();

        }

    }

}

四、文章参考

上面的代码是Dotnet9看 Disign com WPF 大神视频手敲的,下面是大神youtube地址及本实例学习视频。

参考:

Design com WPF : https://www.youtube.com/watch?v=YQ1EJJZBHyE


除非注明,文章均由 Dotnet9 整理发布,欢迎转载。转载请注明本文地址:https://dotnet9.com/2019/12/it-technology/csharp/wpf/csharp-wpf-material-design-ui-navigation-drawer-popup-menu.html如有所收获,请大力转发(能点赞及推荐那是极好的);如觉小编写文不易,欢迎给Dotnet9站点打赏,小编谢谢了;谢谢大家对dotnet技术的关注和支持 。

你可能感兴趣的:(C# WPF抽屉效果实现(C# WPF Material Design UI: Navigation Drawer & PopUp Menu))