title: Data validation in Silverlight with multiple objects
body: <p>My Silverlight form requires to input values for fields in objects of 3 classes that together will make the request for the web service to be invoked</p>
  <p>Code-in-progress for the GUI is the following</p>
  <pre><code>&lt;UserControl x:Class=&quot;ClientSanitaro.MainPage&quot;
  xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
  xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
  xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  mc:Ignorable=&quot;d&quot;
  d:DesignHeight=&quot;450&quot; d:DesignWidth=&quot;600&quot; xmlns:sdk=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk&quot; xmlns:my=&quot;clr-namespace:It.Unina.MasterICT.ClientSanitario.Controls&quot; xmlns:data=&quot;clr-namespace:It.Unina.MasterICT.ClientSanitario.Data&quot;  Loaded=&quot;UserControl_Loaded&quot;&gt;
  
  
  &lt;sdk:TabControl Height=&quot;400&quot; HorizontalAlignment=&quot;Center&quot; Margin=&quot;10,10,0,0&quot; Name=&quot;tabControl&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;550&quot;&gt;        
      &lt;sdk:TabItem Header=&quot;Upload documenti&quot; Name=&quot;tabUpload&quot;&gt;
          &lt;sdk:TabItem.DataContext&gt;
              &lt;data:PazienteGui/&gt;
          &lt;/sdk:TabItem.DataContext&gt;
          
          
          &lt;Grid&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height=&quot;30&quot;/&gt;
                  &lt;RowDefinition Height=&quot;*&quot;/&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Grid Grid.Row=&quot;0&quot;&gt;
                  &lt;Grid.Resources&gt;
                      &lt;Style TargetType=&quot;TextBox&quot;&gt;
                          &lt;Setter Property=&quot;Margin&quot; Value=&quot;2&quot;/&gt;
                      &lt;/Style&gt;
                      &lt;Style TargetType=&quot;sdk:Label&quot;&gt;
                          &lt;Setter Property=&quot;Margin&quot; Value=&quot;2&quot;/&gt;
                      &lt;/Style&gt;
                  &lt;/Grid.Resources&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width=&quot;3*&quot;/&gt;
                      &lt;ColumnDefinition Width=&quot;5*&quot;/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;TextBox Height=&quot;23&quot; HorizontalAlignment=&quot;Left&quot; Name=&quot;txtUploadEndpoint&quot; Width=&quot;300&quot; Grid.Column=&quot;1&quot; VerticalAlignment=&quot;Center&quot; /&gt;
                  &lt;sdk:Label Height=&quot;23&quot; HorizontalAlignment=&quot;Right&quot; Name=&quot;lblUploadEndpoint&quot; VerticalAlignment=&quot;Center&quot; Width=&quot;80&quot; Content=&quot;URL endpoint&quot; Grid.Column=&quot;0&quot; /&gt;
              &lt;/Grid&gt;
  
              &lt;Grid Grid.Row=&quot;1&quot; Margin=&quot;15&quot;&gt;
                  &lt;Grid.Resources&gt;
                      &lt;Style TargetType=&quot;TextBox&quot;&gt;
                          &lt;Setter Property=&quot;Margin&quot; Value=&quot;3&quot;/&gt;
                      &lt;/Style&gt;
                      &lt;Style TargetType=&quot;sdk:Label&quot;&gt;
                          &lt;Setter Property=&quot;Margin&quot; Value=&quot;3&quot;/&gt;
                      &lt;/Style&gt;
                  &lt;/Grid.Resources&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                      &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                      &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                      &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                      &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                      &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                      &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                      &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width=&quot;1*&quot;/&gt;
                      &lt;ColumnDefinition Width=&quot;2*&quot;/&gt;
                      &lt;ColumnDefinition Width=&quot;1*&quot;/&gt;
                      &lt;ColumnDefinition Width=&quot;2*&quot;/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;sdk:Label Content=&quot;Persona&quot; FontWeight=&quot;Bold&quot; HorizontalAlignment=&quot;Left&quot; Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot; Grid.ColumnSpan=&quot;2&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Cognome&quot; Grid.Row=&quot;1&quot; Grid.Column=&quot;0&quot; HorizontalAlignment=&quot;Right&quot;/&gt;
                  &lt;TextBox Name=&quot;txtPersonaCognome&quot; Grid.Row=&quot;1&quot; Grid.Column=&quot;1&quot; HorizontalAlignment=&quot;Stretch&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Nome&quot; Grid.Row=&quot;2&quot; Grid.Column=&quot;0&quot; HorizontalAlignment=&quot;Right&quot;/&gt;
                  &lt;TextBox Name=&quot;txtNome&quot; Grid.Row=&quot;2&quot; Grid.Column=&quot;1&quot; HorizontalAlignment=&quot;Stretch&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Codice Fiscale&quot; Grid.Row=&quot;3&quot; Grid.Column=&quot;0&quot; HorizontalAlignment=&quot;Right&quot; /&gt;
                  &lt;TextBox Name=&quot;txtPersonaCodiceFiscale&quot; Grid.Row=&quot;3&quot; Grid.Column=&quot;1&quot; HorizontalAlignment=&quot;Stretch&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Residenza&quot; Grid.Row=&quot;4&quot; Grid.Column=&quot;0&quot; HorizontalAlignment=&quot;Right&quot;/&gt;
                  &lt;TextBox Name=&quot;txtPazienteResidenza&quot; Grid.Row=&quot;4&quot; HorizontalAlignment=&quot;Stretch&quot; Grid.Column=&quot;1&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Struttura Sanitaria&quot; Grid.Row=&quot;5&quot; Grid.Column=&quot;0&quot; HorizontalAlignment=&quot;Left&quot; Grid.ColumnSpan=&quot;2&quot; FontWeight=&quot;Bold&quot; /&gt;
                  &lt;sdk:Label Content=&quot;Nome&quot; Grid.Row=&quot;6&quot; Grid.Column=&quot;0&quot; HorizontalAlignment=&quot;Right&quot;/&gt;
                  &lt;TextBox Name=&quot;txtStrutturaNome&quot; Grid.Row=&quot;6&quot; Grid.Column=&quot;1&quot; HorizontalAlignment=&quot;Stretch&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Indirizzo&quot; Grid.Row=&quot;7&quot; Grid.Column=&quot;0&quot; HorizontalAlignment=&quot;Right&quot;/&gt;
                  &lt;TextBox Name=&quot;txtStrutturaIndirizzo&quot; Grid.Row=&quot;7&quot; Grid.Column=&quot;1&quot; HorizontalAlignment=&quot;Stretch&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Documento Sanitario&quot; Grid.Row=&quot;0&quot; Grid.Column=&quot;2&quot; FontWeight=&quot;Bold&quot; HorizontalAlignment=&quot;Left&quot; Grid.ColumnSpan=&quot;2&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Contenuto&quot; Grid.Row=&quot;1&quot; Grid.Column=&quot;2&quot; HorizontalAlignment=&quot;Right&quot;/&gt;
                  &lt;ComboBox Name=&quot;cmbTipoContenuto&quot; Grid.Row=&quot;1&quot; Grid.Column=&quot;3&quot; HorizontalAlignment=&quot;Stretch&quot;/&gt;
                  &lt;sdk:Label Content=&quot;Tipo MIME&quot; Grid.Row=&quot;2&quot; Grid.Column=&quot;2&quot; HorizontalAlignment=&quot;Right&quot;/&gt;
                  &lt;TextBlock Name=&quot;lblMimeType&quot; Grid.Row=&quot;2&quot; Grid.Column=&quot;3&quot; HorizontalAlignment=&quot;Stretch&quot; VerticalAlignment=&quot;Center&quot;/&gt;
  
                  &lt;sdk:Label Content=&quot;File&quot; Grid.Row=&quot;4&quot; Grid.Column=&quot;2&quot; HorizontalAlignment=&quot;Right&quot;/&gt;
                  &lt;my:FileUpload Grid.Column=&quot;3&quot; Grid.Row=&quot;4&quot; Grid.RowSpan=&quot;2&quot; HorizontalAlignment=&quot;Stretch&quot; x:Name=&quot;fileUpload&quot; VerticalAlignment=&quot;Top&quot; FileUploaded=&quot;fileUpload_FileUploaded&quot; FileRemoved=&quot;fileUpload_FileRemoved&quot; /&gt;
                  &lt;Button Content=&quot;Upload&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;7&quot; HorizontalAlignment=&quot;Stretch&quot; Name=&quot;btnUpload&quot; VerticalAlignment=&quot;Stretch&quot; Click=&quot;btnUpload_Click&quot; /&gt;
              &lt;/Grid&gt;
          &lt;/Grid&gt;
  
      &lt;/sdk:TabItem&gt;
      &lt;sdk:TabItem Header=&quot;Ricerca documenti&quot; Name=&quot;tabRicerca&quot;&gt;
          &lt;Grid&gt;&lt;/Grid&gt;
      &lt;/sdk:TabItem&gt;
  &lt;/sdk:TabControl&gt;
  </code></pre>
  
  <p>As you see, in the grid I have several fields, all required (with <code>txtCodiceFiscale</code> being 16 alphanumeric chars long). My Service Reference defines 3 major classes: <code>Persona</code> (person) <code>Documento</code> (Document) and <code>StrutturaSanitaria</code> (HealthFacility). Reading around I found that a &quot;better&quot; way of performing validation is through usage of data binding (which I can easily do in classic WinForms), and perhaps it has something to do with the MVVM pattern which I don't master yet.</p>
  <p>I learned that instead of having the form button's Click event validate the code manually the traditional way I can bind the form and its textboxes to properties of a data context object.</p>
  <h2>The problem</h2>
  <p>I need 3 data objects. All examples I found so far show only one object assigned to the root control (in my case I need to bind the object to the first TabItem since the second tab is supposed to display results from web service and will be propely bound to). Is it possible to to bind a control to multiple objects? (I don't know the syntax for specifying multiple data objects) If not, can I at least bind it to a class like this and reference each property in the tree?</p>
  <pre><code>public class DataContainer {
      public Persona Persona{get; set;}
      public Documento Documento {get; set;}
      public StrutturaSanitaria Struttura {get; set;}
  }
  </code></pre>
  <p>It saves me the headache of defining a class that embodies all the values, so at least when I click <code>Submit</code> I have all the objects filled with data.</p>
  <p>Or, do you have other viable solutions that are cheap in code? I'm trying to find some books but I don't have the time to read them all before my deadline. Could someone show me a good tutorial on data validation that helps me <strong>understand</strong> the mechanisms behind it, or just explain them to me concisely?</p>