C # WPF form prompt changes

Micro-channel public number: Dotnet9 , website: Dotnet9 , questions or suggestions, please leave a message Web site;
if you feel Dotnet9 to help you, welcome appreciation

C # WPF form prompt changes

Content Directory

  1. Achieve results
  2. Business scene
  3. Coding
  4. Reference article
  5. Source download

1. To achieve the effect

Form display without making changes
Form display without making changes

Form changes, close the form tips
Form changes, close the form tips

Gif look to a dynamic operation
Gif look to a dynamic operation

2. Business scene

When the form changes and close the window before the examination tips

3. Coding

3.1 Add Nuget library

Use .Net Core 3.1 to create WPF called "ValidateDataChange" solution, add two Nuget libraries: MaterialDesignThemes and MaterialDesignColors.

MaterialDesign control library

3.2 of engineering structures

4 file changes:

  1. App.xaml: Adding MD control style
  2. MainWindow.xaml: the main window to achieve results
  3. MainWindow.xaml.cs: The main window is closed and the background binding verification
  4. Contact.cs: entity bound

3.3 App.xaml introduction MD control style

<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 The main form MainWindow.xaml

Forms display, the MD control as a message prompts 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

Binding data, form validation before the form is closed: using simple hashcode determines whether the binding entity changes.

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

Contact entity class

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. Reference herein

Design com WPF Great God of learning videos: the Validate the Data Change

Open Source Control Library: MaterialDesignInXamlToolkit

site introduction to the MD of open source control library: Controls Introduction

5. Codes

Github Source Download: Download

Except where noted, the article by Dotnet9 finishing release, welcome to reprint.

This article reproduced please specify the address: https: //dotnet9.com/6823.html

welcome attention Dotnet9 Fanger Wei code scanning at the micro-channel public number, this site will promptly push the latest technology

Dotnet9

Guess you like

Origin www.cnblogs.com/Dotnet9-com/p/12155815.html