0

I have a Silverlight form that performs exception-based data validation. I learned how to do this data validation the following way:

Set controls to be validated as follows:

<TextBox Text="{Binding Mode=TwoWay,NotifyOnValidationError=True, Source={StaticResource docSan}, Path= metadati.paziente.residenza, ValidatesOnExceptions=True}"/>

Make the target property work as follows

public new string residenza
    {
        get { return base.residenza; }
        set
        {
            if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
            base.residenza = value;
        }
    }

Where the base class defines a non-validating property in an INotifyPropertyChanged way

Unfortunately VS2010 at design time warns me about the exception for each text box. This doesn't prevent the application from running (it works fine) but it's just annoying.

Somebody knows how to tell VS that it's OK if at design time no value is specified thus the code throws naturally?

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305

1 Answers1

1

If I understand correctly, it is the if ... throw statement in the setter that causes the warnings in the designer?

I think you can use the DesignerProperties.IsInDesignTool to prevent this line from running in design-time:

set
{
    if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
    {
        if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
    }
    base.residenza = value;
}
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
  • Seems it doesn't work. When I open the XAML file I get all the errors in the bottom of the IDE, but if I hit F6 it compiles fine, errors disappear but warnings are shown on top of the editor and I can't click on designed controls. Maybe I understood why: the problem could lie in the **getter** part because the designer wants to set value to the textboxes starting from the data bound object which is obviously null. Maybe I should change the getter and return empty string at design time if value is null but it seems really too odd. Everyone uses data binding today without problems – usr-local-ΕΨΗΕΛΩΝ Jul 02 '12 at 10:38
  • Visual Studio says: Il valore non può essere null. Nome parametro: Text in System.Windows.Controls.TextBox.set_Text(String value) "Value cannot be null. Parameter name: Text" – usr-local-ΕΨΗΕΛΩΝ Jul 02 '12 at 10:39
  • Did you get to the bottom of this? I have not managed to reproduce the error myself yet, but I will give it a few more shots. Hopefully I can come up with a sufficient solution from these attempts. – Anders Gustafsson Jul 07 '12 at 18:30
  • Full code: https://marcus.zighinetto.org/svnroot/masterict/trunk/StrutturaSanitaria2/ – usr-local-ΕΨΗΕΛΩΝ Jul 07 '12 at 21:05