Question

I'm trying to change the default error message in a Silverlight DataGrid when the input is in an incorrect format. For example, you type letters into a numerical field. As you tab away, it says "input is not in a correct format." I've seen how to fix this, and that is to put a validation attribute on it with a custom error message. Problem is, my object is coming from RIA services. It seems to ignore my custom error message from my validation attributes. Is there something I need to do to expose this? Thanks in advance.

Was it helpful?

Solution

Sounds like you don't have the metadata set up for your objects. using metadata for validation in silverlight This will create it for you and bring the annotation to the silverlight project.

OTHER TIPS

Validation attributes/metadata attributes won't help here because the error happens on the control and not on the property. The control is not able to call the setter of type int (or any other numeric type) because the string value cannot be cast. I'd also like to know you can change the default error message...

A possible workaround is to use a custom TextBox that only allows numeric input, that looks something like this:

public class NumericTextBox : TextBox
{
    public NumericTextBox()
    {
        this.KeyDown += new KeyEventHandler(NumericTextBox_KeyDown);
    }

    void NumericTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Back || e.Key == Key.Shift || e.Key == Key.Escape || e.Key == Key.Tab || e.Key == Key.Delete)
            return;

        if (e.Key < Key.D0 || e.Key > Key.D9)
        {
            if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
            {
                    e.Handled = true;
            }
        } 
    }
}

The only solution that works is this (this is on the client side):

public partial class MyEntity        
{
    public string MyField_string
    {
        get
        {
            return MyField.ToString();
        }
        set
        { 
            decimal res = 0;
            var b = Decimal.TryParse(value, out res);
            if (!b)
                throw new ArgumentException("Localized message");
            else
                this.MyField = Math.Round(res, 2);
        }
    }

    partial void OnMyFieldChanged()
    {
        RaisePropertyChanged("MyField_string");
    }
}

And then bind to MyField_string instead of MyField.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top