views:

1184

answers:

2

I'm trying to add Validation to a Silverlight 3.0 TextBox but cannot seem to find and example which is complete, and not missing functionality that makes it work, I have this property:

    ''' <summary>Tag</summary>
    ''' <value>String</value>
    ''' <returns>String</returns>
    ''' <remarks>Contains Validation</remarks>
    Public Property Tag() As String
        Get
            Return _Tag
        End Get
        Set(ByVal Value As String)
            If Value.Length < 1 Or Value.Length > 20 Then
                Throw New Exception("Must be between 1 and 20 characters")
            End If
            _Tag = Value
            NotifyPropertyChanged("Tag")
        End Set
    End Property

This is in a class which implements the INotifyPropertyChanged class, I have this textbox:

<TextBox Name="txtTag" Text="{Binding Path=Tag, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>

In my XAML, what do I need to do to have this use Validation correctly - that is show the "red" tooltip to show the error message, my code just throws the exception, and does not display the message at or work correctly.
I have found examples online, but all seem to miss out a critical piece of functionality that makes this work - what is the reason for this? I just want to do simple validation in Silverlight, I know it is possible so if anyone knows an end-to-end solution to make this work then please post it here.
This is an open-source project so I won't be keeping the solution to myself - so it may help others as this is quite a useful feature - if not poorly explained for me to get it to work.


Edit

Just though I would add that if possible can the Error condition of a Textbox be invoked without using an exception - i.e I do my own validation and then call the error state on the TextBox and show the custom error message, this would be an acceptable alternative. I have the Tag property in a Class, I'm guessing when the exception is thrown it cannot be caught by the TextBox as it is not in the MainPage class, but there is so little documentation, a couple of good examples with no code I'm not sure how this is supposed to work.
Hopefully there is a solution that does not involve re-implementing the error state with coloured borders and custom tooltips, as the functionality is there - I just need to use it!

+1  A: 

Check out this blog on Silverlight Data Binding/Validation:

Data Binding - Data Validation

...it looks like you're missing the Error Event Handler (what does the work when the Exception is thrown). Check the link to see what I'm talking about.

Justin Niessner
I knew I was missing something, I found a few examples online - none of these had that part - assumed prior knowledge I guess - will try this!
RoguePlanetoid
The link appears to be broken..
Yuval Peled
@Yuval: link fixed
Pourquoi Litytestdata
A: 

Your code looks fine. You can add a <ValidationSummary/> to the panel containing the TextBox to see the validation errors. You can also subscribe to the TextBox.BindingValidationError event to see when validation errors occur as in the data-binding. If you debug your code you should be able to first see the exception being thrown and then the event being raised.

Unfortunately you cannot "manually" add validation errors to the TextBox. To get a validation error you will have to throw an exception in the a data-bound property like you do. If you want really fine-grained control of when the validation error occurs you can manually update the binding expression.

Instead of performing your validation in code you can use the attributes in namepace System.ComponentModel.DataAnnotations. For instance, to validate on string length you can use the StringLength attribute:

<StringLength(20, MinimumLength:=1>
Public Property Tag() As String
    Get
        ....
Martin Liversage
I had a feeling that the exception-throw-catch method was the way of doing this, I just need to move my Property from the Class to the MainPage class, so the exception is caught by the bound-property correctly, if this works then I have got it in the wrong place!
RoguePlanetoid