DataValidation in WPF using ValidatesOnExceptions

I want to run a basic data validation sample in WPF using ValidatesOnException, but its simply not working, and as soon as my viewmodel throws ValidationException, my program crashes saying, ValidationException was unhandled by user code.

My View Model is

public class MainViewModel : INotifyPropertyChanged
{
    //INotifyPropertyChaned implementation
    //////////////////////////////////////
    private string stringValue;

    public string StringValue
    {
        get { return stringValue; }
        set
        {
            if (value.Length > 6)
            {
                //The below line throws unhandled exception error??
                throw new ValidationException(String.Format("Value's length is greater than {0}.", value.Length));
            }
            stringValue = value;
            this.OnPropertyChanged("StringValue");
        }
    }
}

My XAML is

<StackPanel x:Name="LayoutRoot" Background="White">
<TextBox x:Name="radMaskedTextInput1" 
                                Width="200"
                                Margin="10"
                                Text="{Binding Path=StringValue, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>

Answers


I ran your code, and when executed under a debugger, yes, the VS debugger stops at the throw, because there is no catch statement that handles that exception.

But when started without debugging, the applications does not crash - the edit box border turns red.

If you want to get rid of the exception, you may change the ViewModel to implement IDataErrorInfo interface instead of throwing exception.

If the exception is interfering with your debugging, you could for example, start throwing a custom exception derived from ArgumentException or ValidationException, and the configure VS to not break when this custom exception is thrown and user-unhandled


Need Your Help

When creating a Drupal 7 content type programmatically, is there a way to add tuples of fields?

drupal-7 drupal-modules

In Drupal 7, I'd like to create a Person content type. A person may have multiple profiles - an administrator should be able to select which profile will be considered the primary one. So, that means

How to Use CONTAINS keyword in Sqlite Database

android sqlite search

what i am trying to use is that i want to select cities from my LocationByCity table in sqlite database each time As user inters search word in a search box,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.