在MahApps控件中,表单加载时的验证事件引发,我需要在单击按钮后显示验证消息

本文关键字:验证 按钮 消息 显示 单击 事件 控件 MahApps 表单 加载 | 更新日期: 2023-09-27 18:25:49

IDataErrorInfo:验证页面何时提交

这里我使用IdataErrorInfo

下面是我的主窗口ViewModel类COde

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MahApps.Metro;
using MetroDemo.Models;
using System.Windows.Input;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
namespace MetroDemo
{
    public class AccentColorMenuData
    {
        public string Name { get; set; }
        public Brush BorderColorBrush { get; set; }
        public Brush ColorBrush { get; set; }
        private ICommand changeAccentCommand;
        public ICommand ChangeAccentCommand
        {
            get { return this.changeAccentCommand ?? (changeAccentCommand = new SimpleCommand { CanExecuteDelegate = x => true, ExecuteDelegate = x => this.DoChangeTheme(x) }); }
        }
        protected virtual void DoChangeTheme(object sender)
        {
            var theme = ThemeManager.DetectAppStyle(Application.Current);
            var accent = ThemeManager.GetAccent(this.Name);
            ThemeManager.ChangeAppStyle(Application.Current, accent, theme.Item1);
        }
    }
    public class AppThemeMenuData : AccentColorMenuData
    {
        protected override void DoChangeTheme(object sender)
        {
            var theme = ThemeManager.DetectAppStyle(Application.Current);
            var appTheme = ThemeManager.GetAppTheme(this.Name);
            ThemeManager.ChangeAppStyle(Application.Current, theme.Item2, appTheme);
        }
    }
    public class MainWindowViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        private readonly IDialogCoordinator _dialogCoordinator;
        int? _integerGreater10Property;
        int? _emptystring;
        private bool _animateOnPositionChange = true;
        public MainWindowViewModel(IDialogCoordinator dialogCoordinator)
        {
            _dialogCoordinator = dialogCoordinator;
            SampleData.Seed();
            // create accent color menu items for the demo
            this.AccentColors = ThemeManager.Accents
                                            .Select(a => new AccentColorMenuData() { Name = a.Name, ColorBrush = a.Resources["AccentColorBrush"] as Brush })
                                            .ToList();
            // create metro theme color menu items for the demo
            this.AppThemes = ThemeManager.AppThemes
                                           .Select(a => new AppThemeMenuData() { Name = a.Name, BorderColorBrush = a.Resources["BlackColorBrush"] as Brush, ColorBrush = a.Resources["WhiteColorBrush"] as Brush })
                                           .ToList();

            Albums = SampleData.Albums;
            Artists = SampleData.Artists;
            FlipViewTemplateSelector = new RandomDataTemplateSelector();
            FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(Image));
            spFactory.SetBinding(Image.SourceProperty, new System.Windows.Data.Binding("."));
            spFactory.SetValue(Image.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
            spFactory.SetValue(Image.StretchProperty, Stretch.Fill);
            FlipViewTemplateSelector.TemplateOne = new DataTemplate()
            {
                VisualTree = spFactory
            };
            FlipViewImages = new string[] { "http://trinities.org/blog/wp-content/uploads/red-ball.jpg", "http://savingwithsisters.files.wordpress.com/2012/05/ball.gif" };
            RaisePropertyChanged("FlipViewTemplateSelector");

            BrushResources = FindBrushResources();
            CultureInfos = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures).ToList();
        }
        public string Title { get; set; }
        public int SelectedIndex { get; set; }
        public List<Album> Albums { get; set; }
        public List<Artist> Artists { get; set; }
        public List<AccentColorMenuData> AccentColors { get; set; }
        public List<AppThemeMenuData> AppThemes { get; set; }
        public List<CultureInfo> CultureInfos { get; set; }
        public int? IntegerGreater10Property
        {
            get { return this._integerGreater10Property; }
            set
            {
                if (Equals(value, _integerGreater10Property))
                {
                    return;
                }
                _integerGreater10Property = value;
                RaisePropertyChanged("IntegerGreater10Property");
            }
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        DateTime? _datePickerDate;
        public DateTime? DatePickerDate
        {
            get { return this._datePickerDate; }
            set
            {
                if (Equals(value, _datePickerDate))
                {
                    return;
                }
                _datePickerDate = value;
                RaisePropertyChanged("DatePickerDate");
            }
        }



        bool _magicToggleButtonIsChecked = true;
        public bool MagicToggleButtonIsChecked
        {
            get { return this._magicToggleButtonIsChecked; }
            set
            {
                if (Equals(value, _magicToggleButtonIsChecked))
                {
                    return;
                }
                _magicToggleButtonIsChecked = value;
                RaisePropertyChanged("MagicToggleButtonIsChecked");
            }
        }
        private bool _quitConfirmationEnabled;
        public bool QuitConfirmationEnabled
        {
            get { return _quitConfirmationEnabled; }
            set
            {
                if (value.Equals(_quitConfirmationEnabled)) return;
                _quitConfirmationEnabled = value;
                RaisePropertyChanged("QuitConfirmationEnabled");
            }
        }

            }
        }

            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the PropertyChanged event if needed.
        /// </summary>
        /// <param name="propertyName">The name of the property that changed.</param>
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public string this[string columnName]
        {
            get
            {
                if (columnName == "IntegerGreater10Property" && this.IntegerGreater10Property < 10)
                {
                    return "Number is not greater than 10!";
                }
                if (columnName == "DatePickerDate" && this.DatePickerDate == null)
                {
                    return "No Date given!";
                }
                if (columnName == "FirstName")
                {
                    if (string.IsNullOrEmpty(FirstName) || FirstName.Length < 3)
                        return "Please Enter  First Name";
                }
                if (columnName == "LastName")
                {
                    if (string.IsNullOrEmpty(FirstName) || FirstName.Length < 3)
                        return "Please Enter Last Name";
                }
                return null;
            }
        }
        public string Error { get { return string.Empty; } }
        private ICommand singleCloseTabCommand;
        public ICommand SingleCloseTabCommand
        {
            get
            {
                return this.singleCloseTabCommand ?? (this.singleCloseTabCommand = new SimpleCommand
                {
                    CanExecuteDelegate = x => true,
                    ExecuteDelegate = async x =>
                    {
                        await ((MetroWindow) Application.Current.MainWindow).ShowMessageAsync("Closing tab!", string.Format("You are now closing the '{0}' tab", x));
                    }
                });
            }
        }
        private ICommand neverCloseTabCommand;
        public ICommand NeverCloseTabCommand
        {
            get { return this.neverCloseTabCommand ?? (this.neverCloseTabCommand = new SimpleCommand { CanExecuteDelegate = x => false }); }
        }

        private ICommand showInputDialogCommand;
        public ICommand ShowInputDialogCommand
        {
            get
            {
                return this.showInputDialogCommand ?? (this.showInputDialogCommand = new SimpleCommand
                {
                    CanExecuteDelegate = x => true,
                    ExecuteDelegate = x =>
                    {
                        _dialogCoordinator.ShowInputAsync(this, "From a VM", "This dialog was shown from a VM, without knowledge of Window").ContinueWith(t => Console.WriteLine(t.Result));
                    }
                });
            }
        }


        private ICommand showProgressDialogCommand;
        public ICommand ShowProgressDialogCommand
        {
            get
            {
                return this.showProgressDialogCommand ?? (this.showProgressDialogCommand = new SimpleCommand
                {
                    CanExecuteDelegate = x => true,
                    ExecuteDelegate = x => RunProgressFromVm()
                });
            }
        }





    }
}

下面是我的XAml代码

  <TextBox  Name="txt_LA_FirstName"
                         Controls:TextBoxHelper.Watermark="First Name"
                         Controls:TextBoxHelper.UseFloatingWatermark="True"
                         ToolTip="First Name" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Stretch"  Text="{Binding FirstName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" />

事件在表单加载时引发并显示错误消息在用户输入任何值之前>我需要在按钮单击事件之后显示错误消息

在MahApps控件中,表单加载时的验证事件引发,我需要在单击按钮后显示验证消息

之所以得到它,是因为索引器的内容是静态的,所以View在绑定过程中对其进行验证。不过,您应该只在属性更改时对其进行验证。

您应该为带有Add/Remove方法的错误字典提供一个后备字段,比如这个

    private readonly Dictionary<string, string> errors = new Dictionary<string, string>();
    public string this[string columnName]
    {
        get
        {
            string errorMessage = null;
            if (errors.TryGetValue(columnName, errorMessage) 
            {
                return errorMessage;
            }
            return null;
        }
    }
    protected void AddErrorMessage(string columnName, string errorMessage) 
    {
        errors[columnName] = errorMessage;
    }
    protected void RemoveErrorMessage(string columnName) 
    {
         if(errors.ContainsKey(columnName)) 
        {
            errors.Remove(columnName);
        }
    }
    protected virtual void Validate() 
    {
            errors.Clear();
            if (this.IntegerGreater10Property < 10)
            {
                this.AddErrorMessage("IntegerGreater10Property", "Number is not greater than 10!");
            }
            if (this.DatePickerDate == null)
            {
                this.AddErrorMessage("DatePickerDate", "No Date given!");
            }
            if (string.IsNullOrEmpty(FirstName) || FirstName.Length < 3)
            {
                this.AddErrorMessage("FirstName", "Please Enter  First Name");
            }
            if (string.IsNullOrEmpty(LastName) || Lastname.Length < 3)
            {
                this.AddErrorMessage("LastName", "Please Enter Last Name");
            }
    }

然后用RemoveMessage("FirstName")删除它们,或者在验证时将其清除。当然,您应该在基类中实现这一点,以避免反复重复此代码,并只覆盖ViewModel类

中的Validate()方法

感谢您的回复

即使我尝试过,我也通过在Bool变量上创建并在MainwindowView模型类中将其声明为False来解决问题。

除非Bool变量为True,否则不会验证

按钮点击事件I将使布尔变量为True,然后在按钮click 上启动Validates

if (Enable)
                {

                    if (columnName == "IntegerGreater10Property" && this.IntegerGreater10Property < 10)
                    {
                        return "Number is not greater than 10!";
                    }
                    if (columnName == "DatePickerDate" && this.DatePickerDate == null)
                    {
                        return "No Date given!";
                    }
                    if (columnName == "FirstName")
                    {
                        if (string.IsNullOrEmpty(FirstName) || FirstName.Length < 3)
                            return "Please Enter  First Name";
                    }
                    if (columnName == "LastName")
                    {
                        if (string.IsNullOrEmpty(FirstName) || FirstName.Length < 3)
                            return "Please Enter Last Name";
                    }
                }
                return null;

下面是我的按钮点击事件(在按钮上,我将启用Bool属性为真正的

 var x = (MainWindowViewModel)DataContext;
            x.Enable = true;
            var binding = txt_LA_FirstName.GetBindingExpression(TextBox.TextProperty);
            binding.UpdateSource();