未做修改的表单展示
表单变化,关闭窗体提示
来个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:绑定的实体
<Application x:Class="ValidateDataChange.App"3.4 主窗体 MainWindow.xaml
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>
表单展示,使用MD控件的Snackbar作为消息提示
<Window x:Class="ValidateDataChange.MainWindow"3.5 MainWindow.xaml.cs
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>
数据绑定,窗体关闭前表单验证:简单使用hashcode判断绑定实体是否有变化。
using System;3.6 Contact.cs
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();
}
}
}
联系人实体类
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();
}
}
}