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

原文: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

 1 <Application x:Class="PopUpAndNav.App"
 2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4              xmlns:local="clr-namespace:PopUpAndNav"
 5              StartupUri="MainWindow.xaml">
 6     <Application.Resources>
 7         <ResourceDictionary>
 8             <ResourceDictionary.MergedDictionaries>
 9                 <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml"/>
10                 <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/>
11                 <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml"/>
12                 <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml"/>
13             </ResourceDictionary.MergedDictionaries>
14         </ResourceDictionary>
15     </Application.Resources>
16 </Application>

App.xaml

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

 1 <Window x:Class="PopUpAndNav.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:PopUpAndNav"
 7         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
 8         mc:Ignorable="d" Foreground="White"
 9         Title="MainWindow" Height="600" Width="1080" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">
10     <Window.Resources>
11         <Storyboard x:Key="MenuOpen">
12             <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu">
13                 <EasingDoubleKeyFrame KeyTime="0" Value="60"/>
14                 <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="200"/>
15             </DoubleAnimationUsingKeyFrames>
16         </Storyboard>
17         <Storyboard x:Key="MenuClose">
18             <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu">
19                 <EasingDoubleKeyFrame KeyTime="0" Value="200"/>
20                 <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="60"/>
21             </DoubleAnimationUsingKeyFrames>
22         </Storyboard>
23     </Window.Resources>
24
25     <Window.Triggers>
26         <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonOpenMenu">
27             <BeginStoryboard Storyboard="{StaticResource MenuOpen}"/>
28         </EventTrigger>
29         <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonCloseMenu">
30             <BeginStoryboard Storyboard="{StaticResource MenuClose}"/>
31         </EventTrigger>
32     </Window.Triggers>
33
34     <Grid Background="LightGray">
35         <Grid x:Name="GridTitle" Height="60" VerticalAlignment="Top" Background="#FF1368BD" MouseDown="GridTitle_MouseDown">
36             <TextBlock Text="C# WPF 抽屉效果" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22"/>
37             <StackPanel VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Right">
38                 <TextBlock Text="Dotnet9.com" VerticalAlignment="Center" FontSize="18"/>
39                 <materialDesign:PopupBox Foreground="White" Margin="10" PlacementMode="BottomAndAlignRightEdges" StaysOpen="False">
40                     <StackPanel Width="150">
41                         <Button Content="账号"/>
42                         <Button Content="设置"/>
43                         <Button Content="帮助"/>
44                         <Separator/>
45                         <Button x:Name="ButtonPopUpLogout" Content="Logout" Click="ButtonPopUpLogout_Click"/>
46                     </StackPanel>
47                 </materialDesign:PopupBox>
48             </StackPanel>
49         </Grid>
50         <Grid x:Name="GridMenu" Width="60" HorizontalAlignment="Left" Background="#FF1B3861">
51             <StackPanel>
52                 <Grid Height="150" Background="White">
53                     <Button x:Name="ButtonCloseMenu" Width="60" Height="60" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" HorizontalAlignment="Right" Visibility="Collapsed" Click="ButtonCloseMenu_Click">
54                         <materialDesign:PackIcon Kind="ArrowLeft" Foreground="#FF1B3861" Width="25" Height="25"/>
55                     </Button>
56                     <Button x:Name="ButtonOpenMenu" Width="60" Height="60" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" HorizontalAlignment="Right" Click="ButtonOpenMenu_Click">
57                         <materialDesign:PackIcon Kind="Menu" Foreground="#FF1B3861" Width="25" Height="25"/>
58                     </Button>
59                 </Grid>
60                 <ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled" Foreground="#FF1368BD">
61                     <ListViewItem Height="60">
62                         <StackPanel Orientation="Horizontal">
63                             <materialDesign:PackIcon Kind="ViewDashboard" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/>
64                             <TextBlock Text="首页" VerticalAlignment="Center" Margin="20 10"/>
65                         </StackPanel>
66                     </ListViewItem>
67                     <ListViewItem Height="60">
68                         <StackPanel Orientation="Horizontal">
69                             <materialDesign:PackIcon Kind="Pencil" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/>
70                             <TextBlock Text="创建" VerticalAlignment="Center" Margin="20 10"/>
71                         </StackPanel>
72                     </ListViewItem>
73                     <ListViewItem Height="60">
74                         <StackPanel Orientation="Horizontal">
75                             <materialDesign:PackIcon Kind="Ticket" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/>
76                             <TextBlock Text="售票" VerticalAlignment="Center" Margin="20 10"/>
77                         </StackPanel>
78                     </ListViewItem>
79                     <ListViewItem Height="60">
80                         <StackPanel Orientation="Horizontal">
81                             <materialDesign:PackIcon Kind="Message" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/>
82                             <TextBlock Text="信息" VerticalAlignment="Center" Margin="20 10"/>
83                         </StackPanel>
84                     </ListViewItem>
85                     <ListViewItem Height="60">
86                         <StackPanel Orientation="Horizontal">
87                             <materialDesign:PackIcon Kind="GithubBox" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/>
88                             <TextBlock Text="GitHub" VerticalAlignment="Center" Margin="20 10"/>
89                         </StackPanel>
90                     </ListViewItem>
91                 </ListView>
92             </StackPanel>
93         </Grid>
94     </Grid>
95 </Window>

MainWindow.xaml

后台MainWindow.xaml.cs,主要是处理窗体关闭事件及抽屉菜单的展开与折叠:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace PopUpAndNav
17 {
18     /// <summary>
19     /// Interaction logic for MainWindow.xaml
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26         }
27
28         private void ButtonPopUpLogout_Click(object sender, RoutedEventArgs e)
29         {
30             Application.Current.Shutdown();
31         }
32
33         private void ButtonOpenMenu_Click(object sender, RoutedEventArgs e)
34         {
35             ButtonOpenMenu.Visibility = Visibility.Collapsed;
36             ButtonCloseMenu.Visibility = Visibility.Visible;
37         }
38
39         private void ButtonCloseMenu_Click(object sender, RoutedEventArgs e)
40         {
41             ButtonOpenMenu.Visibility = Visibility.Visible;
42             ButtonCloseMenu.Visibility = Visibility.Collapsed;
43         }
44
45         private void GridTitle_MouseDown(object sender, MouseButtonEventArgs e)
46         {
47             DragMove();
48         }
49     }
50 }

MainWindow.xaml.cs

 

四、文章参考

上面的代码是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技术的关注和支持 。

原文地址:https://www.cnblogs.com/lonelyxmas/p/12065765.html

时间: 2024-10-08 22:40:28

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

Android群英传笔记——第十二章:Android5.X 新特性详解,Material Design UI的新体验

Android群英传笔记--第十二章:Android5.X 新特性详解,Material Design UI的新体验 第十一章为什么不写,因为我很早之前就已经写过了,有需要的可以去看 Android高效率编码-第三方SDK详解系列(二)--Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能 这一章很多,但是很有趣,也是这书的最后一章知识点了,我现在还在考虑要不要写这个拼图和2048的案例,在此之前,我们先来玩玩Android5.X的新特性吧!

Freebie: Material Design UI Kit

点这里 Following the guidelines laid out by Google, this free UI kit has been designed so that you can easily get your next Material Design project kickstarted quickly. Every element, widget, button and form within the kit can be fully customised as per

Material Design UI Widgets

Android L 开发人员预览支持库提供两个新的Widgets,RecyclerView和CardView. 使用这两个Widgets能够显示复杂的Listview和卡片布局.这两个Widgets默认使用Material design. RecyclerView RecyclerView是一个更高级柔性版本号的Listview.RecyclerView是一个能包括非常多视图的容器.它能完美的处理循环和滚动.在item动态变化的Listview使用RecyclerView. RecyclerVi

基于Vue的Material Design UI库 Vue-Carbon

vue-carbon,基于 vue 开发material design 风格的移动端 WEB UI 库, 设计资源参考 CARBON FrameWork7(app开发公司). 安装 目前只使用 npm 安装,和使用 webpack 项目的应用(基础教程qkxue.net) npm install vue-carbon --save import Vue from 'vue' import VueCarbon from 'vue-carbon' Vue.use(VueCarbon) 简单使用 例如

进入Material Design时代

由于本文引用了大量官方文档.图片资源,以及开源社区的Lib和相关图片资源,因此在转载的时候,务必注明来源,如果使用资源请注明资源的出处,尊重版权,尊重别人的劳动成果,谢谢! Material Design 官方Material Design详细介绍文档:http://www.google.com/design/spec/material-design/introduction.html 关于Material Design是Android 5.0系统的重头戏,并在以后App中将成为一种设计标准,而

【Android】进入Material Design时代

由于本文引用了大量官方文档.图片资源,以及开源社区的Lib和相关图片资源,因此在转载的时候,务必注明来源,如果使用资源请注明资源的出处,尊重版权,尊重别人的劳动成果,谢谢! Material Design 官方Material Design详细介绍文档:http://www.google.com/design/spec/material-design/introduction.html Material Design是Android 5.0系统的重头戏,并在以后App中将成为一种设计标准,而且随

Android Material Design : Ripple Effect水波波纹荡漾的视觉交互设计

?? Android Material Design : Ripple Effect水波波纹荡漾的视觉交互设计 Android Ripple Effect波纹荡漾效果,是Android Material Design视觉设计引入的一种交互设计效果简言之:当点击某个view时候,view会出现像水波波纹一样的荡漾传播效果.在最新版的Android如Android 5.0或以上版本中默认具有该效果,但在低版本Android中没有,如果需要向下兼容低版本设备,则需要自己写代码实现,实现步骤: 第1步:

Android5.0美不胜收的新特性 Material Design

Google提出了全新的设计规范Material Design,扁平化的设计,加上明亮的色彩,有一种美不胜收的感觉. Material Design翻译过来叫做"材料设计",Material Design是多种元素组合在一起形成一个层次的效果,有主题.新的控件.动画,那么使用Material Design要注意些什么呢?下面来看看使用的注意点: 1.保证兼容性,可以兼容市场占有率高的低版本系统 下面来初步认识一下Material Design的设计规范: 1.使用Material De

Material Design风格SearchView实现语音搜索功能《IT蓝豹》

Material Design风格SearchView实现语音搜索功能 Material Design风格SearchView实现语音搜索功能,弹出录音对话框效果也很漂亮.但是这份代码支持在5.0以上的手机才能看到效果.VoiceActivity 类实现录制语音功能.自定义一个MaterialSearchView searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {