views:

40

answers:

1

This may be a bit winded because I am new to wpf. I have created a partial class for an entity in my L2S class that is primarily used for validation. It implements the onchanging and onvalidate methods. I am trying to use the MVVM pattern, and in a window/view I have set the datacontext in the xaml:

<Window.DataContext>
    <vm:StartViewModel />
</Window.DataContext>

when a user leaves a required field in the view blank, the onchanging event of the partial class is fired when I close the form, not when I save the data. So, if a user leaves the textbox blank, the old value is retained and the onchaging method is fired, but I have no idea how to alert the user of the resulting error. here is my onchanging code in the partial class:

    partial void Ondocument_titleChanging(string value)
    {
        if (value.Length == 0)
            throw new Exception("Document title is required.");
        if (value.Length > 256)
            throw new Exception("Document title cannot be longer than 256 characters.");
    }

throwing an exception doesn't notify the user of the error. it just allows the form to close and rejects the changes to the textbox.

hope this makes sense...

edit: this example was taken from Scott Guthries article here: http://aspalliance.com/1427_LINQ_to_SQL_Part_5__Binding_UI_using_the_ASPLinqDataSource_Control.5

A: 

ok, figured it out. I needed to add the following to the controls binding:

Text="{Binding Path=CurrentDocument.document_title, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"