微信公众号:Dotnet9,网站:Dotnet9,问题或建议,请网站留言;

如果您觉得Dotnet9对您有帮助,欢迎赞赏

C# WPF 表单更改提示

内容目录

  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控件样式

<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 的微信公众号,本站会及时推送最新技术文章


C# WPF 表单更改提示的更多相关文章

  1. jQuery Label Better – 友好的表单输入框提示插件

    jQuery Label Better 帮助你标记您的表单输入域,带有美丽的动画效果而且不占用空间.这个插件的独特之处在于所有你需要做的就是添加一个占位符文本,只有当用户需要它的时候才显示标签. 您可 ...

  2. JEECG 3.7.8 新版表单校验提示风格使用&升级方法(validform 新风格漂亮,布局简单)

    JEECG 表单校验采用的是validform,默认的校验提示需要占用页面布局,提示效果较传统.jeecg这个自定义的校验提示风格,不占用页面布局,提示效果也更美观,简单易用,让表单看起来更漂亮!!! ...

  3. Laravel表单验证提示设置多语言

    默认表单提示是英文的,我们可以安装语言包构建多语言环境. 根据版本选择命令 For Laravel 7.x : run composer require caouecs/laravel-lang:~6 ...

  4. espcms简约版的表单,提示页,搜索列表页

    模板/lib/form.html <script type="text/javascript" src="{%$rootdir%}js/My97DatePicker ...

  5. ie表单提交提示下载文件

    使用jquery的ajaxform提交form表单 如果在html中多了   enctype ="multipart/form-data"   属性值 提交时就会在ie8中提示下载 ...

  6. wpf表单验证

    在做表单的,需要对User提交数据做验证,wpf与silverlight 都提供自带的验证机制,但是只是验证,并不能在提交时提供详细的信息,此时可使用 依赖属性将错误信息整合进自定义错误集合中,即可在 ...

  7. form表单验证提示语句

    <input id="idcardcode" name="idcardcode" class="form-control"       ...

  8. MVC 表单提交提示:已添加了具有相同键的项。

    MVC:页面提交的时候报如下错误: 解决方案: 这个Model 里面定义了重复的字段so~~~

  9. H5: 表单验证失败的提示语

    前言     前端的童鞋在写页面时, 都不可避免的总会踩到表单验证这个坑. 这时候, 我们就要跪了, 因为要写一堆js来检查. 但是自从H5出现后, 很多常见的表达验证, 它都已经帮我们实现了, 让我 ...

随机推荐

  1. Dapper系列 作者:懒懒的程序员一枚

    Dapper 第一篇简单介绍什么是小巧玲珑?Dapper如何工作安装需求方法参数结果常用类型 Dapper 第二篇 Execute 方法介绍描述存储过程Insert语句Update语句Delete语句 ...

  2. 威联通(NAS)搭建个人图床

    名词解释: 图床:一般是指储存图片的服务器,有国内和国外之分.国外的图床由于有空间距离等因素决定访问速度很慢影响图片显示速度.国内也分为单线空间.多线空间和cdn加速三种. 更详细的内容,请左转查看百 ...

  3. 研发协同平台持续集成之Jenkins实践

    导读 研发协同平台有两个核心目标,一是提高研发效率 ,二是提高研发质量,要实现这两个核心目标,实现持续集成是关键之一. 什么是持续集成 在<持续集成>一书中,对持续集成的定义如下:持续集成 ...

  4. 蓝桥杯ALGO-1,区间k大数查询

    #include<stdio.h> int devide(long a[], int low, int high) { long key = a[high]; while (low< ...

  5. 1,Python爬虫环境的安装

    前言 很早以前就听说了Python爬虫,但是一直没有去了解:想着先要把一个方面的知识学好再去了解其他新兴的技术. 但是现在项目有需求,要到网上爬取一些信息,然后做数据分析.所以便从零开始学习Pytho ...

  6. mybatis 通过配置父类数据源连接和关闭数据,进行junit单元测试

    来源:https://blog.csdn.net/Bigbig_lyx/article/details/80646005 解决问题,单元测试没经过单独配置,每个测试方法中要添加配置数据源 一:配置父类 ...

  7. Myeclipse maven项目转web项目

    右键点击项目,选择project facets,或者在properties选择,点“Convert to faceted from...” 勾选java和Dynamic Web Module 选项 接 ...

  8. python实现自动点赞

    1.思路通过pyautogui可以实现鼠标点击.滚动鼠标.截屏等操作.由此功能实现打开页面,进行点赞.aircv可以从大图像获得小图像的位置,利用pyautogui截屏得到的图片,可以在页面获取到每一 ...

  9. jQuery 源码解析(三十一) 动画模块 便捷动画详解

    jquery在$.animate()这个接口上又封装了几个API,用于进行匹配元素的便捷动画,如下: $(selector).show(speed,easing,callback)        ;如 ...

  10. MySQL索引底层数据结构

    一.何为索引? 1.索引是帮助数据库高效获取数据的排好序的数据结构. 2.索引存储在文件中. 3.索引建多了会影响增删改效率. (下面这张图为计算机组成原理内容,每查询一次索引节点,都会进行一次磁盘I ...