2015-12-05 5 views
0

IDataErrorInfo: Validating when page submitted在MahApps控制驗證事件的形式加載的我需要顯示驗證消息的時間提出了按鈕後點擊

這裏我使用IDataErrorInfo的

下面是我的MainwindowViewModel類代碼

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}" /> 

事件擡高表單負載並顯示錯誤消息用戶輸入任何值前>我需要顯示按鈕單擊事件後的錯誤消息

回答

0

您得到它的原因是因爲您的索引器的內容是靜態的,所以View在綁定過程中驗證它。您應該只在屬性更改時驗證它。

你應該有一個支持字段爲錯誤使用添加/刪除方法字典,這樣

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")或清除它掛在確認刪除它們。你應該在一個基類,當然實現這一點,以避免一遍又一遍重複的代碼,只是重寫Validate()方法在您的視圖模型類

0

感謝您的回覆

即使我嘗試它,我已經解決了它通過在MainwindowView模型類中創建Bool變量並將其聲明爲False。

除非及直至布爾變量爲真,它不會驗證

在按鈕單擊事件,我將啓用布爾變量設置爲true然後就按一下按鈕開始void驗證

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; 

下面是我的按鈕點擊事件(按鈕上我將啓用布爾屬性爲真

var x = (MainWindowViewModel)DataContext; 
      x.Enable = true; 

      var binding = txt_LA_FirstName.GetBindingExpression(TextBox.TextProperty); 
      binding.UpdateSource(); 
相關問題