Validation L2S question
Posted
by user158020
on Stack Overflow
See other posts from Stack Overflow
or by user158020
Published on 2009-09-04T02:49:50Z
Indexed on
2010/04/10
5:03 UTC
Read the original article
Hit count: 294
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
© Stack Overflow or respective owner