Error Template Design

前端 未结 1 961
清酒与你
清酒与你 2021-02-08 02:58

It seems like I read another question / answer on this site about this issue but I cannot recall what the answer was and now I cannot find the original post.

I am not a

1条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 03:12

    Here's a solution adapted from Josh Smith's article on Binding to (Validation.Errors)[0] without Creating Debug Spew.

    The trick is to define a DataTemplate to render the ValidationError object and then use a ContentPresenterto display the error message. If there is no error, then the ContentPresenter will not be displayed.

    Below, I have shared the code of the sample app that I created.

    Without errors With errors

    Here is the XAML:

    
        
            
                
                    
                
            
            
            
    
            
            
            

    The code behind file:

    namespace WpfApplication1
    {
        /// 
        /// Interaction logic for MainWindow.xaml
        /// 
        public partial class MainWindow : Window
        {
            private ViewModel _ViewModel = null;
    
            public MainWindow()
            {
                InitializeComponent();
                _ViewModel = new ViewModel();
                DataContext = _ViewModel;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                _ViewModel.Validate = true;
                _ViewModel.OnPropertyChanged("Text1");
                _ViewModel.OnPropertyChanged("Text2");
            }
        }
    }
    

    The ViewModel:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace WpfApplication1
    {
        public class ViewModel : INotifyPropertyChanged, IDataErrorInfo
        {
            private string _Text1;
            public string Text1
            {
                get { return _Text1; }
                set
                {
                    _Text1 = value;
                    OnPropertyChanged("Text1");
                }
            }
    
            private string _Text2;
            public string Text2
            {
                get { return _Text2; }
                set
                {
                    _Text2 = value;
                    OnPropertyChanged("Text2");
                }
            }
    
            public bool Validate { get; set; }
    
            #region INotifyPropertyChanged Implemenation
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            #endregion
    
            #region IDataErrorInfo Implementation
            public string Error
            {
                get { return null; }
            }
    
            public string this[string columnName]
            {
                get
                {
                    string errorMessage = string.Empty;
                    if (Validate)
                    {
                        switch (columnName)
                        {
                            case "Text1":
                                if (Text1 == null)
                                    errorMessage = "Text1 is mandatory.";
                                else if (Text1.Trim() == string.Empty)
                                    errorMessage = "Text1 is not valid.";
                                break;
                            case "Text2":
                                if (Text2 == null)
                                    errorMessage = "Text2 is mandatory.";
                                else if (Text2.Trim() == string.Empty)
                                    errorMessage = "Text2 is not valid.";
                                break;
                        }
                    }
                    return errorMessage;
                }
            }
            #endregion
        }
    }
    

    0 讨论(0)
提交回复
热议问题