C# WPF抽屉效果实现

时间如流水,只能流去不流回!

点赞再看,养成习惯,这是您给我创作的动力!

本文 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

<Application x:Class="PopUpAndNav.App"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"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml"/><ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/><ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml"/><ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>

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

<Window x:Class="PopUpAndNav.MainWindow"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"><Window.Resources><Storyboard x:Key="MenuOpen"><DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu"><EasingDoubleKeyFrame KeyTime="0" Value="60"/><EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="200"/></DoubleAnimationUsingKeyFrames></Storyboard><Storyboard x:Key="MenuClose"><DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu"><EasingDoubleKeyFrame KeyTime="0" Value="200"/><EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="60"/></DoubleAnimationUsingKeyFrames></Storyboard></Window.Resources><Window.Triggers><EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonOpenMenu"><BeginStoryboard Storyboard="{StaticResource MenuOpen}"/></EventTrigger><EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ButtonCloseMenu"><BeginStoryboard Storyboard="{StaticResource MenuClose}"/></EventTrigger></Window.Triggers><Grid Background="LightGray"><Grid x:Name="GridTitle" Height="60" VerticalAlignment="Top" Background="#FF1368BD" MouseDown="GridTitle_MouseDown"><TextBlock Text="C# WPF 抽屉效果" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22"/><StackPanel VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Right"><TextBlock Text="Dotnet9.com" VerticalAlignment="Center" FontSize="18"/><materialDesign:PopupBox Foreground="White" Margin="10" PlacementMode="BottomAndAlignRightEdges" StaysOpen="False"><StackPanel Width="150"><Button Content="账号"/><Button Content="设置"/><Button Content="帮助"/><Separator/><Button x:Name="ButtonPopUpLogout" Content="Logout" Click="ButtonPopUpLogout_Click"/></StackPanel></materialDesign:PopupBox></StackPanel></Grid><Grid x:Name="GridMenu" Width="60" HorizontalAlignment="Left" Background="#FF1B3861"><StackPanel><Grid Height="150" Background="White"><Button x:Name="ButtonCloseMenu" Width="60" Height="60" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" HorizontalAlignment="Right" Visibility="Collapsed" Click="ButtonCloseMenu_Click"><materialDesign:PackIcon Kind="ArrowLeft" Foreground="#FF1B3861" Width="25" Height="25"/></Button><Button x:Name="ButtonOpenMenu" Width="60" Height="60" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" HorizontalAlignment="Right" Click="ButtonOpenMenu_Click"><materialDesign:PackIcon Kind="Menu" Foreground="#FF1B3861" Width="25" Height="25"/></Button></Grid><ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled" Foreground="#FF1368BD"><ListViewItem Height="60"><StackPanel Orientation="Horizontal"><materialDesign:PackIcon Kind="ViewDashboard" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/><TextBlock Text="首页" VerticalAlignment="Center" Margin="20 10"/></StackPanel></ListViewItem><ListViewItem Height="60"><StackPanel Orientation="Horizontal"><materialDesign:PackIcon Kind="Pencil" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/><TextBlock Text="创建" VerticalAlignment="Center" Margin="20 10"/></StackPanel></ListViewItem><ListViewItem Height="60"><StackPanel Orientation="Horizontal"><materialDesign:PackIcon Kind="Ticket" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/><TextBlock Text="售票" VerticalAlignment="Center" Margin="20 10"/></StackPanel></ListViewItem><ListViewItem Height="60"><StackPanel Orientation="Horizontal"><materialDesign:PackIcon Kind="Message" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/><TextBlock Text="信息" VerticalAlignment="Center" Margin="20 10"/></StackPanel></ListViewItem><ListViewItem Height="60"><StackPanel Orientation="Horizontal"><materialDesign:PackIcon Kind="GithubBox" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/><TextBlock Text="GitHub" VerticalAlignment="Center" Margin="20 10"/></StackPanel></ListViewItem></ListView></StackPanel></Grid></Grid>
</Window>

后台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
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>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技术的关注和支持 。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/312641.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

seaborn.heatmap概述

点击这里查看官网文档 如果是单纯看数据分布情况&#xff0c;则直接调用包即可。 如果是想看特征两两之间的相似图&#xff0c;则需先求一个相关系数矩阵。 参考样例 import numpy as np import seaborn as sns import matplotlib.pyplot as pltnp.random.seed(10) sns.set_t…

视频来了!Visual Studio Online 东半球首秀 @ .NET Conf 2019 中国峰会

2019 年 11 月 9 日&#xff0c; .NET Conf 2019 中国峰会于上海中谷小南国花园酒店举行&#xff0c;全国的 .NET 大咖相聚上海。这次我演讲的主题是《Visual Studio Code —— .NET 开发利器》。除了聊了聊韩老师开发的 .NET Core Test Explorer&#xff0c;还重点聊了聊 Visu…

PAT-A Sign In and Sign Out

这题挺水的&#xff0c;就是要注意char数组长度至少设置为16&#xff0c;设成15由于存在结尾结束符&#xff0c;会导致长度为15的ID无法存入数组中。 #include<bits/stdc.h> using namespace std;int main() {int n;scanf("%d", &n);char be[16], ed[16];…

开源性能监控工具APM之Skywalking和Pinpoint的实测对比

作者&#xff1a;zollty&#xff0c;资深程序员和架构师&#xff0c;私底下是个爱折腾的技术极客&#xff0c;架构师社区合伙人&#xff01;零&#xff0c;什么是APM&#xff1f;APM&#xff08;ApplicationPerformance Management&#xff09;是一种应用性能监控工具&#xff…

PAT-A Maximum Subsequence Sum

是道常规的动态规划题&#xff0c;不过我有被卡住&#xff08;丢脸 需要考虑当序列整体求和为0的情况。 #include<bits/stdc.h> using namespace std;int main() {int k;vector<int> v;scanf("%d", &k);int temp;bool all_negative true;for(int i…

.Net Core 3.1,这匹黑马,2020年值得所有程序员重视!

强人掌舵 重回巅峰2014年萨蒂亚纳德拉接棒史蒂芬鲍尔默成为微软掌门人&#xff0c;五年的时间&#xff0c;纳德拉将夕阳西下、停滞不前的微软重新带回巅峰状态。2019年&#xff0c;微软今年挤下苹果&#xff0c;成为2019年全球市值最高企业&#xff0c;终止苹果过去7年来的龙头…

GridSearchCV和RandomizedSearchCV(以SVM为例)

GridSearchCV参考代码 CC []gammas []for i in range(-5, 16, 2):CC.append(2 ** i)for i in range(3, -16, -2):gammas.append(2 ** i)param_grid {"C": CC, "gamma": gammas}gs GridSearchCV(SVC(probabilityTrue), param_grid, cv10)gs.fit(x, y)pr…

为什么你没有选择xamarin?

点击上方“dotNET全栈开发”&#xff0c;“设为星标”加“星标★”&#xff0c;每天11.50&#xff0c;好文必达全文约900字&#xff0c;预计阅读时间11分钟注&#xff1a;本篇文章只是关于xamarin的一个分享&#xff0c;没有xamarin技术深入的地方2019 年&#xff0c;注定会是 …

新款 Azure .NET SDK 如何设定 Content-Type

点击上方蓝字关注“汪宇杰博客”导语前不久我写了一篇《尝鲜新版 Azure .NET SDK》之后&#xff0c;使用了一段时间发现没爆&#xff0c;于是今天决定把博客的图片存储从旧版 SDK 迁移到新版 SDK&#xff0c;结果小收福报。Content-Type上传到 Azure Blob 的文件必须设置正确的…

使用Java查询Sql Server数据库

import java.sql.*;public class Connect {static Connection con;static Statement stmt;public static void doConnect() throws SQLException{String JDriver "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎String connectDB "jdbc:sql…

聊聊程序员的成长与价值提升

一、 回顾我的职场体会我的职场生涯开始于十年前&#xff0c;一直在中小企业发展&#xff0c;未曾有幸到沿海那些高速发展的互联网公司工作&#xff0c;也就错过了互联网的大时代。但这不影响我的正常工作生活&#xff0c;事实上无论你身在何处&#xff0c;或在哪家公司&#x…

交叉验证中n_jobs=-1并行运算

scikit_learn包中的cross_val_score&#xff08;&#xff09;是支持并行运算&#xff0c;但这并不是说只要让n_jobs-1就能让CPU使用率接近100%。这要取决于交叉验证的折数cv&#xff0c;假如折数cv是n&#xff0c;这意味着最多只能使用n个物理CPU。 score1 cross_val_score(c…

本地Jupyter连接远程linux服务器

连接的前提是本地装好了Jupyter&#xff0c;以及远程的服务器也配置好了。 先连接远程的Linux服务器&#xff0c;输入脚本命令&#xff1a; jupyter notebook得到执行结果&#xff1a; [I 14:29:11.290 NotebookApp] Serving notebooks from local directory: /home/ubuntu …

PowerBI 2019.12更新完美收官2019

PowerBI 的2019年12月更新来了。终于 PowerBI 完成了在 2019 年的进化。对于 12 月来说&#xff0c;最大的更新就是提供了主题设置&#xff0c;这使得我们彻底摆脱编写一个从来都不应该编写的JSON来设置主题文件。下面我们分别来详细介绍。自定义主题首先你需要打开预览&#x…

Task.CompletedTask和Task.Result小记

在任何返回Task的方法中&#xff0c;如果可以在不进行异步的情况下计算结果&#xff0c;则最好避免使用Task.Run。例如&#xff0c;一个简短的计算函数&#xff0c;或者测试中返回了一个预先计算过的结果&#xff0c;则无需使用Task.Run。例如&#xff0c;定义了一个返回Task的…

深度学习初步理解

梯度下降概念&#xff1a; 我们用到一种名为梯度下降&#xff08;gradient descent&#xff09;的方法&#xff0c; 这种方法几乎可以优化所有深度学习模型。 它通过不断地在损失函数递减的方向上更新参数来降低误差。 梯度下降最简单的用法是计算损失函数&#xff08;数据集中…

Dapr 运用之集成 Asp.Net Core Grpc 调用篇

前置条件&#xff1a; 《Dapr 运用》改造 ProductService 以提供 gRPC 服务从 NuGet 或程序包管理控制台安装 gRPC 服务必须的包Grpc.AspNetCore配置 Http/2gRPC 服务需要 Http/2 协议public static IHostBuilder CreateHostBuilder(string[] args) {return Host.CreateDefault…

蓝桥杯 印章拿金币

今日心得 对于大数组的定义要放在main函数外&#xff0c;否则会报错认真思考动态规划的边界设置&#xff0c;不是机械地设置1或0 动态转移方程&#xff1a; dp[i][j] dp[i-1][j]*j/n dp[i-1][j-1]*(n-j1)/n;参考代码 #include<bits/stdc.h> using namespace std;int…

ASP.NET Core Web API 最佳实践指南

原文地址&#xff1a; ASP.NET-Core-Web-API-Best-Practices-Guide介绍当我们编写一个项目的时候&#xff0c;我们的主要目标是使它能如期运行&#xff0c;并尽可能地满足所有用户需求。但是&#xff0c;你难道不认为创建一个能正常工作的项目还不够吗&#xff1f;同时这个项目…

蓝桥杯 杨辉三角形

试题&#xff1a; 如果我们按从上到下、从左到右的顺序把所有数排成一列&#xff0c;可以得到如下数列&#xff1a; 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, 给定一个正整数 NN&#xff0c;请你输出数列中第一次出现 NN 是在第几个数&#xff1f; 思路&#xff1a; 组合…