title: how to correctly bind a View control to a ViewModel List (WPF MVVM)
body: <p>I need to bind a List to a UniformGrid into a WP Window using WPF MVVM.</p>
  
  <p>I had in mind to do something like this:</p>
  
  <p>Into my VM:</p>
  
  <pre><code>   private List&lt;Rat&gt; rats;
          private UniformGrid uniformGrid;
          public List&lt;Rat&gt; Rats
          {
              get { return rats; }
              set
              {
  
                  if (rats != value)
                      {
                      //update local list value
                          rats = value;
  
                      //create View UniformGrid
                          if (uniformGrid == null)
                              uniformGrid = new UniformGrid() { Rows=10};
                          else
                              uniformGrid.Children.Clear();
                      foreach(Rat rat in value)
                      {
                          StackPanel stackPanel = new StackPanel();
                          Ellipse ellipse = new Ellipse(){Height=20, Width=20, Stroke= Brushes.Black};
                          if (rat.Sex== SexEnum.Female)
                              ellipse.Fill= Brushes.Pink;
                          else 
                               ellipse.Fill= Brushes.Blue;
  
                          stackPanel.Children.Add(ellipse  );
                          TextBlock textBlock = new TextBlock();
                          textBlock.Text= rat.Name + " (" + rat.Age +")";
                          stackPanel.Children.Add( textBlock  );
                          uniformGrid.Children.Add(stackPanel);
                      }
  
  
                          OnPropertyChanged("Rats");
                      }            
              }
          }
  </code></pre>
  
  <p>The VM is correctly informed when the list needs to be refreshed into the view via an event.
  So at this point I would need my View to be correctly bound to the VM. I made it this way:</p>
  
  <pre><code> &lt;GroupBox x:Name="GB_Rats" Content="{Binding Rats}"  Header="Rats" HorizontalAlignment="Left" Height="194" Margin="29,10,0,0" VerticalAlignment="Top" Width="303"&gt;
  </code></pre>
  
  <p>Is this the correct global approch?</p>
  
  <p>Concretely, when attempting to run the code, this line fails to execute:</p>
  
  <pre><code> uniformGrid = new UniformGrid() { Rows=10};
  </code></pre>
  
  <p>-></p>
  
  <pre><code>An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationCore.dll
  Additional information: The calling thread must be STA, because many UI components require this.
  </code></pre>
  
  <p>This lets me think that I should not proceed this way from a MVVM point of view.</p>
  
  <p>Thx for your kind assistance.</p>