当前位置: 首页 > news >正文

WPF+MVVM案例实战(二十一)- 制作一个侧边弹窗栏(AB类)

文章目录

  • 1、案例效果
  • 1、侧边栏分类
  • 2、AB类侧边弹窗实现
    • 1.文件创建
    • 2、样式代码与功能代码实现
    • 3、功能代码实现
  • 3 运行效果
  • 4、源代码获取


1、案例效果

在这里插入图片描述

1、侧边栏分类

  • A类 :左侧弹出侧边栏
  • B类 :右侧弹出侧边栏
  • C类 :顶部弹出侧边栏
  • D类 :底部弹出侧边栏

2、AB类侧边弹窗实现

1.文件创建

打开项目 Wpf_Examples ,在Views 文件夹下创建窗体界面文件 PopPanelWindow.xaml 。如下所示:
在这里插入图片描述

2、样式代码与功能代码实现

1、弹出床的两种实现,一种是鼠标移上按钮就触发,点击弹窗面板关闭效果。第二种是 单击按钮弹出侧边栏,鼠标点击其他空白区域退出弹窗栏。这里我们把第一种做成右侧弹出栏,第二种做成左侧弹出栏。

PopPanelWindow.xaml 界面样式如下所示:

<Window x:Class="Wpf_Examples.Views.PopPanelWindow"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:Wpf_Examples.Views"mc:Ignorable="d"Title="PopPanelWindow" Height="450" Width="600" Background="#2B2B2B"><Grid><Button Width="250" Height="30" Content="鼠标移上按弹出右侧边栏"><Button.Triggers><!--  用按钮的鼠标进入事件来触发进入动画  --><EventTrigger RoutedEvent="MouseEnter"><BeginStoryboard><!--  进入动画  --><Storyboard Storyboard.TargetName="border" Storyboard.TargetProperty="RenderTransform.X"><DoubleAnimation From="120"To="0"Duration="0:0:1"><DoubleAnimation.EasingFunction><!--  设置缓动模式和振荡次数  --><BackEase Amplitude="0.5" EasingMode="EaseOut" /></DoubleAnimation.EasingFunction></DoubleAnimation></Storyboard></BeginStoryboard></EventTrigger></Button.Triggers></Button><!--  侧边栏  --><Border x:Name="border"Width="200"HorizontalAlignment="Right"Background="LightSkyBlue"><!--位移效果-->  <Border.RenderTransform><TranslateTransform x:Name="tt" X="200" /></Border.RenderTransform><Border.Effect><DropShadowEffect Direction="205"Opacity="0.6"ShadowDepth="1"Color="Black" /></Border.Effect><Border.Triggers><!--  鼠标的左键按下事件来触发退出动画  --><EventTrigger RoutedEvent="MouseLeftButtonDown"><BeginStoryboard><!--  退出动画  --><Storyboard Storyboard.TargetName="tt" Storyboard.TargetProperty="X"><DoubleAnimation From="0"To="200"Duration="0:0:0.8" /></Storyboard></BeginStoryboard></EventTrigger></Border.Triggers></Border><Button Width="150" Height="30" Margin="0 80 0 0" Content="单击弹出左侧侧边栏" Click="Button_Click"/><!--  左侧边栏  --><Border x:Name="leftPanel"Width="200"HorizontalAlignment="Left"Background="LightSkyBlue"Visibility="Collapsed"><!--  位移效果  --><Border.RenderTransform><TranslateTransform x:Name="toRight" X="0" /></Border.RenderTransform><Border.Effect><DropShadowEffect Direction="225" Opacity="0.6" ShadowDepth="1" Color="Black" /></Border.Effect></Border></Grid>
</Window>

3、功能代码实现

PopPanelWindow.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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;namespace Wpf_Examples.Views
{/// <summary>/// PopPanelWindow.xaml 的交互逻辑/// </summary>public partial class PopPanelWindow : Window{public PopPanelWindow(){InitializeComponent();// 注册窗口的鼠标点击事件this.MouseLeftButtonDown += PopPanelWindow_MouseLeftButtonDown;}private void Button_Click(object sender, RoutedEventArgs e){// 触发进入动画leftPanel.Visibility = Visibility.Visible;Storyboard enterStoryboard = new Storyboard();DoubleAnimation enterAnimation = new DoubleAnimation{From = 0,To = 200,Duration = TimeSpan.FromSeconds(1),EasingFunction = new BackEase { Amplitude = 0.5, EasingMode = EasingMode.EaseOut }};Storyboard.SetTarget(enterAnimation, toRight);Storyboard.SetTargetProperty(enterAnimation, new PropertyPath("X"));enterStoryboard.Children.Add(enterAnimation);enterStoryboard.Begin();}private void PopPanelWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){// 检查点击的位置是否在侧边栏之外if (!leftPanel.IsMouseOver && leftPanel.Visibility == Visibility.Visible){// 触发退出动画Storyboard exitStoryboard = new Storyboard();DoubleAnimation exitAnimation = new DoubleAnimation{From = 200,To = 0,Duration = TimeSpan.FromSeconds(0.6)};Storyboard.SetTarget(exitAnimation, toRight);Storyboard.SetTargetProperty(exitAnimation, new PropertyPath("X"));exitStoryboard.Children.Add(exitAnimation);exitStoryboard.Completed += (s, ev) => leftPanel.Visibility = Visibility.Collapsed;exitStoryboard.Begin();}}}
}

3 运行效果

在这里插入图片描述

4、源代码获取

源代码是包含上下左右侧边弹出栏全部功能的。代码下载后直接运行即可。
CSDN下载链接:WPF实战案例系列-侧边弹窗等案例

http://www.lryc.cn/news/479031.html

相关文章:

  • linux中怎样登录mysql数据库
  • 深入理解 Linux 内存管理:free 命令详解
  • 指针万字超级最强i解析与总结!!!!!
  • 告别生硬电子音,这款TTS软件让语音转换更自然动听
  • CORS(跨域资源共享)和SOP(同源策略)
  • 【系统设计】数据库压缩技术详解:从基础到实践(附Redis内存优化实战案例)
  • 基于SpringBoot的“乐校园二手书交易管理系统”的设计与实现(源码+数据库+文档+PPT)
  • debian11安装最新rabbitmq
  • 三十三、Python基础语法(面向对象其他语法-下)
  • 简单又便宜的实现电脑远程开机唤醒方法
  • Flutter鸿蒙next 状态管理框架对比分析
  • Vue Router进阶详解
  • 进程的控制
  • 基于C语言实现的图书管理系统
  • 删除 需要来自XXXX的权限才能对此文件夹进行更改 文件的解决办法
  • ARM base instruction -- ccmp (immediate)
  • 高德 阿里231滑块 分析
  • Unity 的 WebGL 构建中资源图片访问方式
  • WinForms 中使用 MVVM 模式构建应用:实现登录页面、页面导航及 SQLite 数据库连接完整框架搭建过程
  • Chrome调试工具(查看CSS属性)
  • MQTT从入门到精通之MQTT入门
  • Hadoop生态系统主要包括哪些组件以及它们的作用
  • OpenResty 1.27.1.1 已经正式发布
  • 定高虚拟列表:让大数据渲染变得轻松
  • python request与grequests该如何选择
  • Unity3D UI 拖拽
  • 介绍一下memcpy(c基础)
  • 【网络面试篇】HTTP(2)(笔记)——http、https、http1.1、http2.0
  • python-23-一篇文章帮你理解Python推导式
  • WPF中如何简单的使用CommunityToolkit.Mvvm创建一个项目并进行 增删改查