Home Forums WPF controls Xceed Toolkit Plus for WPF PropertyGrid validation

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • drgrumpy
    Participant
    Post count: 1
    #43887 |

    How do I set up the PropertyGrid to display an error condition (like the WinForms ErrorProvider or a red highlight) when data is invalid? I currently have my model class implementing INotifyDataErrorInfo and it’s ValidateProperty method is triggered when the value is changed through the PropertyGrid. I then use the Validator class to check if the value is valid (in this case, not empty) and I set my errors. How do I get the PropertyGrid to respond to the invalid value? Is there a binding that I need to add? Do I need to use some other validation scheme entirely?

    Fawzi [Xceed]
    Member
    Post count: 722

    Hi,

    Nothing special to do. We don’t have your code, so we build up a sample.

    When the PropertyItem is modified in the PropertyGrid, the model class property will be updated, its validate method called (checking for empty string) and when an error is added, the PropertyGrid will show a red rectangle around the PropertyItem (along with a Tooltip when the mouse is over the PropertyItem name).

    Here’s the sample :
    <xctk:PropertyGrid SelectedObject=”{Binding}” />

    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    this.DataContext = new MyData();
    }
    }

    public class MyData : INotifyPropertyChanged, INotifyDataErrorInfo
    {
    private const string ErrorString = “Property Name should not be null”;
    private Dictionary<string, List<string>> _fieldNameToErrors = new Dictionary<string, List<string>>();

    #region Name Property

    public string Name
    {
    get
    {
    return _name;
    }
    set
    {
    if( _name != value )
    {
    _name = value;
    this.IsNameValid( value );
    this.RaisePropertyChanged( “Name” );
    }
    }
    }

    private string _name;

    private void IsNameValid( string name )
    {
    if( string.IsNullOrEmpty( name ) )
    {
    this.AddError( “Name”, MyData.ErrorString, true );
    }
    else
    {
    this.RemoveError( “Name”, MyData.ErrorString );
    }
    }

    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged( string propertyName )
    {
    if( propertyName == null )
    propertyName = string.Empty;

    if( this.PropertyChanged == null )
    return;

    this.PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
    }

    #endregion

    #region INotifyDataErrorInfo Members

    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

    public IEnumerable GetErrors( string propertyName )
    {
    if( string.IsNullOrWhiteSpace( propertyName ) )
    return _fieldNameToErrors.Values;

    List<string> errors = null;

    _fieldNameToErrors.TryGetValue( propertyName, out errors );

    return errors;
    }

    public bool HasErrors
    {
    get
    {
    return ( _fieldNameToErrors.Count > 0 );
    }
    }

    private void AddError( string propertyName, string error, bool isWarning )
    {
    List<string> errors = null;

    if( !_fieldNameToErrors.TryGetValue( propertyName, out errors ) )
    {
    errors = new List<string>();
    _fieldNameToErrors.Add( propertyName, errors );
    }

    if( !errors.Contains( error ) )
    {
    if( isWarning )
    {
    errors.Add( error );
    }
    else
    {
    errors.Insert( 0, error );
    }

    this.RaiseErrorsChanged( propertyName );
    }
    }

    private void RemoveError( string propertyName, string error )
    {
    List<string> errors = null;

    if( _fieldNameToErrors.TryGetValue( propertyName, out errors ) )
    {
    errors.Remove( error );

    if( errors.Count == 0 )
    {
    _fieldNameToErrors.Remove( propertyName );
    this.RaiseErrorsChanged( propertyName );
    }
    }
    }

    public void RaiseErrorsChanged( string propertyName )
    {
    if( this.ErrorsChanged != null )
    this.ErrorsChanged( this, new DataErrorsChangedEventArgs( propertyName ) );
    }

    #endregion
    }

    cmkessell
    Participant
    Post count: 3

    Hi,

    Is the above sample code complete (e.g. is the XAML part available)? I’m trying to get validation working with your propertygrid, and using the above code I’ve managed to detect errors but a red box is being drawn around the entire propertygrid rather than around the individual erroneous item(s).

    I suspect I haven’t configured the XAML correctly. For example, should the propertygrid’s Validation.Errors property be used in some way?

    Thanks

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.