This is a migrated thread and some comments may be shown as answers.

Verification using DataAnnotations - Unhanded Exceptions

5 Answers 191 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Tyler
Top achievements
Rank 1
Tyler asked on 22 Apr 2016, 10:35 PM

Hello,

I'm trying to use DataAnnotations to validate fields on my PropertyGrid (which is using AutoGeneratePropertyDefinitions). I used the instructions here as reference: 

http://docs.telerik.com/devtools/wpf/controls/radpropertygrid/features/validation#validating-through-dataannotations 

...and was able to get pretty far. However, when a property fails validation, I get an Unhandled Exception Error when the ValidationException is thrown. Is there a simple example project someone could provide that is able to use the PropertyGrid w/AutoGeneratePropertyDefinitions, ValidationExceptions & DataAnnotations correctly?

 

Thank you!

Tyler

5 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 27 Apr 2016, 09:11 AM
Hello Tyler,

The behavior you've described is expected as that's how the ValidatesOnException binding works.
This issue is caused by the Visual Studio debugger which stops at the throw because there is no catch statement that handles that exception. If you run this without the debugger or you continue after the exception is thrown it should validate as expected.

If this is a problem, you could consider using IDataErrorInfo instead of exceptions for the validation, or start throwing a custom exception derived from ArgumentException or ValidationException, and then configure VS to not break when this custom exception is thrown and user-unhandled.

I hope you find this information helpful.

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Tyler
Top achievements
Rank 1
answered on 27 Apr 2016, 04:04 PM

Hey Dilyan,

Thank you for your reply, I recently switched to using IDataErrorInfo instead, and it sounds like it will accomplish the same result. (Though I miss using the DataAnnotations for validation, so clean!)

One side question-- are the error tooltip messages stored anywhere that I can easily access? I would like to display these validation error messages in a different format, but am not sure how to accomplish this.

Thank you,

Tyler

0
Dilyan Traykov
Telerik team
answered on 28 Apr 2016, 08:24 AM
Hello Tyler,

I'm attaching a sample project where I've added a handler for the PropertyGrid's Error event and introduced an ObservableCollection<ValidationError> to store all the errors in.

Here's the full code:

public MainWindow()
{
    InitializeComponent();
    propertyGrid.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(OnBindingValidationError));
}
 
private void OnBindingValidationError(object sender, RoutedEventArgs e)
{
    var args = e as ValidationErrorEventArgs;
    if (args.Action == ValidationErrorEventAction.Added)
    {
        this.Results.Add(args.Error);
    }
    else
    {
        this.Results.Remove(args.Error);
    }
}
 
private ObservableCollection<ValidationError> results;
 
public ObservableCollection<ValidationError> Results
{
    get
    {
        if (this.results == null)
        {
            this.results = new ObservableCollection<ValidationError>();
        }
        return results;
    }
}

I hope you find this helpful.

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Tyler
Top achievements
Rank 1
answered on 28 Apr 2016, 11:44 PM

Thank you for your latest reply Dilyan (which I don't immediately see on this page, but I was notified via email and was able to see it)

This is helpful, thank you. However I've hit another speed bump: I'm using a MVVM architecture, and am unsure how to set the bindings on the PropertyGrid's Validation.ErrorEvent in my XAML View so I can correctly delegate to my ViewModel (binding the ErrorEvent to the handling command). I've been attempting this most of the day to no avail.

For example (in this simplified snippet), I could do something like this to bind the LostFocus event to a specific command:

<telerik:RadPropertyGrid x:Name="propertyGrid">
  <telerik:EventToCommandBehavior.EventBindings>
     <telerik:EventBinding Command="{Binding LostFocusCommand}"
        PassEventArgsToCommand="True"
        EventName="LostFocus" />
  </telerik:EventToCommandBehavior.EventBindings>
</telerik:RadPropertyGrid>

I can't seem to find an equivalent method to do this with Validation.ErrorEvent

Anything is appreciated, thank you for the help so far

Tyler

0
Dilyan Traykov
Telerik team
answered on 03 May 2016, 07:04 AM
Hello Tyler,

Since Validation. is an Attached Event, it will not work with the EventToCommandBehavior.

You will need to find an alternative way to bind to the Validation.Error event. I believe you will find these two materials helpful:
Validation.error event in event command
EventToCommand with attached event

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
PropertyGrid
Asked by
Tyler
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Tyler
Top achievements
Rank 1
Share this question
or