博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# WPF 表单更改提示
阅读量:4031 次
发布时间:2019-05-24

本文共 4324 字,大约阅读时间需要 14 分钟。

微信公众号:Dotnet9,网站:Dotnet9,问题或建议,请网站留言; 如果您觉得Dotnet9对您有帮助,欢迎赞赏

Dotnet9.com

内容目录

  1. 实现效果

  2. 业务场景

  3. 编码实现

  4. 本文参考

  5. 源码下载

1.实现效果

未做修改的表单展示 

表单变化,关闭窗体提示 

来个Gif动态操作看看 

2.业务场景

表单修改后,关闭窗体前检查提示

3.编码实现

3.1 添加Nuget库

使用 .Net Core 3.1 创建名为“ValidateDataChange”的WPF解决方案,添加两个Nuget库:MaterialDesignThemes和MaterialDesignColors。

MaterialDesign控件库 

3.2 工程结构

4个文件变动:

  1. App.xaml:添加MD控件样式

  2. MainWindow.xaml:主窗口实现效果

  3. MainWindow.xaml.cs:主窗口后台绑定及关闭验证

  4. Contact.cs:绑定的实体

3.3 App.xaml引入MD控件样式

3.4 主窗体 MainWindow.xaml

表单展示,使用MD控件的Snackbar作为消息提示

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{    ///     /// Interaction logic for MainWindow.xaml    ///     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 的微信公众号,本站会及时推送最新技术文章

你可能感兴趣的文章
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
[LeetCode By Python] 2 Add Two Number
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]9. Palindrome Number
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>