微信公眾號: "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", "[email protected]", "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 的微信公眾號,本站會及時推送最新技術文章