C# WPF 表单更改提示
程序员文章站
2022-07-04 22:25:48
微信公众号: "Dotnet9" ,网站: "Dotnet9" ,问题或建议,请网站留言; "如果您觉得Dotnet9对您有帮助,欢迎赞赏" C WPF 表单更改提示 内容目录 1. 实现效果 2. 业务场景 3. 编码实现 4. 本文参考 5. 源码下载 1.实现效果 未做修改的表单展示 表单变化 ......
微信公众号:dotnet9,网站:dotnet9,问题或建议,请网站留言;
如果您觉得dotnet9对您有帮助,欢迎赞赏
c# wpf 表单更改提示
内容目录
- 实现效果
- 业务场景
- 编码实现
- 本文参考
- 源码下载
1.实现效果
未做修改的表单展示
表单变化,关闭窗体提示
来个gif动态操作看看
2.业务场景
表单修改后,关闭窗体前检查提示
3.编码实现
3.1 添加nuget库
使用 .net core 3.1 创建名为“validatedatachange”的wpf解决方案,添加两个nuget库:materialdesignthemes和materialdesigncolors。
materialdesign控件库
3.2 工程结构
4个文件变动:
- app.xaml:添加md控件样式
- mainwindow.xaml:主窗口实现效果
- mainwindow.xaml.cs:主窗口后台绑定及关闭验证
- contact.cs:绑定的实体
3.3 app.xaml引入md控件样式
<application x:class="validatedatachange.app" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:validatedatachange" 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.deeppurple.xaml"/> <resourcedictionary source="pack://application:,,,/materialdesigncolors;component/themes/recommended/accent/materialdesigncolor.blue.xaml"/> </resourcedictionary.mergeddictionaries> </resourcedictionary> </application.resources> </application>
3.4 主窗体 mainwindow.xaml
表单展示,使用md控件的snackbar作为消息提示
<window x:class="validatedatachange.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:validatedatachange" mc:ignorable="d" xmlns:materialdesign="http://materialdesigninxaml.net/winfx/xaml/themes" title="编辑联系人" height="500" width="400" resizemode="noresize" fontfamily="roboto" fontsize="14" windowstartuplocation="centerscreen" closing="window_closing"> <grid> <grid.rowdefinitions> <rowdefinition height="100"/> <rowdefinition height="*"/> </grid.rowdefinitions> <materialdesign:colorzone mode="primarymid" grid.row="0" verticalalignment="stretch"> <textblock text="联系人" verticalalignment="center" margin="20" fontsize="30"/> </materialdesign:colorzone> <stackpanel margin="10 30" grid.row="1"> <grid> <materialdesign:packicon kind="face" verticalalignment="bottom" margin="2 12" foreground="{binding borderbrush, elementname=textboxname}"/> <textbox x:name="textboxname" margin="5" materialdesign:hintassist.hint="名字" padding="8 0 0 0" text="{binding name}" style="{staticresource materialdesignfloatinghinttextbox}"/> </grid> <grid> <materialdesign:packicon kind="at" verticalalignment="bottom" margin="2 12" foreground="{binding borderbrush, elementname=textboxemail}"/> <textbox x:name="textboxemail" margin="5" materialdesign:hintassist.hint="邮件" padding="8 0 0 0" text="{binding email}" style="{staticresource materialdesignfloatinghinttextbox}"/> </grid> <grid> <stackpanel orientation="horizontal" verticalalignment="bottom" margin="2 10"> <materialdesign:packicon kind="facebook" foreground="{binding borderbrush, elementname=textboxfacebook}"/> <textblock text="facebook.com/" foreground="{binding borderbrush, elementname=textboxfacebook}"/> </stackpanel> <textbox x:name="textboxfacebook" margin="5" materialdesign:hintassist.hint="facebook" padding="54 0 0 0" text="{binding facebook}" style="{staticresource materialdesignfloatinghinttextbox}"/> </grid> </stackpanel> <button grid.rowspan="2" margin="50 72" horizontalalignment="right" verticalalignment="top" style="{staticresource materialdesignfloatingactionaccentbutton}" click="button_click"> <materialdesign:packicon kind="contentsave"/> </button> <materialdesign:snackbar grid.row="1" horizontalalignment="stretch" x:name="snackbarunsavedchanges" verticalalignment="bottom"> <materialdesign:snackbarmessage content="有未保存的更改,是否放弃修改?" actioncontent="放弃" actionclick="snackbarmessage_actionclick"/> </materialdesign:snackbar> </grid> </window>
3.5 mainwindow.xaml.cs
数据绑定,窗体关闭前表单验证:简单使用hashcode判断绑定实体是否有变化。
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 validatedatachange { /// <summary> /// interaction logic for mainwindow.xaml /// </summary> public partial class mainwindow : window { int hash; bool discardchanges; public mainwindow() { initializecomponent(); discardchanges = false; var contact = new contact("dotnet9", "632871194@qq.com", "dotnet9"); hash = contact.gethashcode(); this.datacontext = contact; } private void window_closing(object sender, system.componentmodel.canceleventargs e) { if (this.datacontext.gethashcode() != hash && !discardchanges) { snackbarunsavedchanges.isactive = true; e.cancel = true; } } private void button_click(object sender, routedeventargs e) { //保存数据 } private void snackbarmessage_actionclick(object sender, routedeventargs e) { snackbarunsavedchanges.isactive = false; discardchanges = true; this.close(); } } }
3.6 contact.cs
联系人实体类
using system; using system.collections.generic; using system.componentmodel; using system.text; namespace validatedatachange { internal class contact : inotifypropertychanged { public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string info) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(info)); } } private string name; public string name { get { return name; } set { name = value; notifypropertychanged("name"); } } private string email; public string email { get { return email; } set { email = value; notifypropertychanged("email"); } } private string facebook; public string facebook { get { return facebook; } set { facebook = value; notifypropertychanged("facebook"); } } public contact(string name, string email, string facebook) { this.name = name; this.email = email; this.facebook = facebook; } public override int gethashcode() { return (name + email + facebook).gethashcode(); } } }
4.本文参考
design com wpf 大神的学习视频:validate data change
开源控件库:materialdesigninxamltoolkit
本站对md开源控件库的介绍:
5.代码下载
github源码下载:下载
除非注明,文章均由 dotnet9 整理发布,欢迎转载。
转载请注明本文地址:https://dotnet9.com/6823.html
欢迎扫描下方二维码关注 dotnet9 的微信公众号,本站会及时推送最新技术文章