title: combobox not bound to my value
body: <p>I know, there are a lot of examples but I'm not getting it to work.</p>
  
  <p>I have a Silverlight Mask where I dynamically add UIElements like Textboxes or ComboBoxes. So far, this is working fine. Now I'm trying to set the ComboBox Selected Item from my database values.</p>
  
  <p>I store the information for generation in my own classes.</p>
  
  <pre><code>public class Metadata
  {
      public int? MetadataId { get; set; }
      public string Name { get; set; }
      public int Width { get; set; }
      public int Height { get; set; }
      public int Column { get; set; }
      public MetaDataType MetaDataType { get; set; }
      public List&lt;MetadataData&gt; MetadataData { get; set; }
  }
  
   public class MetadataData
  {
      public int MetadataDataId { get; set; }
      public int MetadataId { get; set; }
      public string Description { get; set; }
  }
  
  
  public partial class MetadataStore
  {
      public Guid MetadataStoreId { get; set; }
      public Guid ObjectId { get; set; }
      public Guid ModuleTypeId { get; set; }
      public int MetadataSetId { get; set; }
      public int? MetadataSetSetId { get; set; }
      public int MetadataId { get; set; }
      public string Description { get; set; }
      public int? RowId { get; set; }
      public DataContainer.Profile userProfile { get; set; }
  }
  </code></pre>
  
  <p>My selected value stores the MetadataDataId as a String in my MetadataStore in the field Description. I'm generating the UIElements based on the MetaData class in code-behind like this</p>
  
  <pre><code>switch ((MetaDataDataType)metadata.MetaDataType.MetaDataTypeId)
          {
              case MetaDataDataType.String:
                  frmElement = new TextBox() { Name = String.Format("dynCtrl_{0}_{1}", metadata.MetadataId, metadata.Name)
                      AcceptsReturn = false, Margin = new Thickness(4),
                      HorizontalAlignment = HorizontalAlignment.Left,
                      Height = metadata.Height, Width = metadata.Width,
                      Style = App.Current.Resources["TextBoxStyleFlat"] as Style };
                  frmElement.SetBinding(TextBox.TextProperty,
                      new Binding { Mode = BindingMode.TwoWay, Path = new PropertyPath(metadata.Name) });
                  break;
  
              case MetaDataDataType.SingleSelection:
                  frmElement = new ComboBox()
                  {
                      Name = String.Format("dynCtrl_{0}_{1}", metadata.MetadataId, metadata.Name),
                      Margin = new Thickness(4),
                      HorizontalAlignment = HorizontalAlignment.Left,
                      Style = App.Current.Resources["ComboBoxStyleFlat"] as Style,
                      ItemsSource = metadata.MetadataData,
                      DisplayMemberPath = "Description",
                      //SelectedItem = metadata.Name,
                      Height = metadata.Height,
                      Width = metadata.Width
                  };
  
                  frmElement.SetBinding(ComboBox.SelectedValueProperty, new Binding { Mode = BindingMode.TwoWay, Path = new PropertyPath(metadata.Name) });
                  frmElement.SetBinding(ComboBox.SelectedValuePathProperty, new Binding { Mode = BindingMode.TwoWay, Path = new PropertyPath("MetadataDataId") });
                  default:
                      break;
  }
  </code></pre>
  
  <p>I thought I could make it work with an <code>Dictionary&lt;string, object&gt;</code></p>
  
  <pre><code>var p = new Dictionary&lt;string, object&gt;();
  p[mData.Metadata.Name] = Convert.ToInt32(item.Description);
  </code></pre>
  
  <p>but as it didn't work, I ended up with generating a <code>DataTable</code> and selecting the first element which gets me an <code>Object</code> with my desired dynamic properties.</p>
  
  <pre><code>TempObject.Testprop
  TempObject.Nutzen
  </code></pre>
  
  <p>The DataSourceCreator is used from <a href="http://blog.bodurov.com/How-to-Bind-Silverlight-DataGrid-From-IEnumerable-of-IDictionary/" rel="nofollow">http://blog.bodurov.com/How-to-Bind-Silverlight-DataGrid-From-IEnumerable-of-IDictionary/</a>. </p>
  
  <pre><code>internal static void generateDataContext(List&lt;MetadataSetMetadata&gt; metadataSetMetadata, List&lt;MetadataStore&gt; metadataStore, ref Grid viewGrid)
      {
          if (metadataStore != null &amp;&amp; metadataStore.Count &gt; 0)
          {
  
              dynamic expObj = new ExpandoObject();
              var p = new Dictionary&lt;string, object&gt;();
              foreach (var item in metadataStore.Where(x =&gt; x.MetadataSetSetId == null))
              {
                  var mData = metadataSetMetadata.FirstOrDefault(x =&gt; x.Metadata != null &amp;&amp; x.Metadata.MetadataId.Equals(item.MetadataId));
  
                  switch ((MetaDataDataType)mData.Metadata.MetaDataType.MetaDataTypeId)
                  {
                      case MetaDataDataType.String:
                      case MetaDataDataType.LongString:
                          p[mData.Metadata.Name] = item.Description;
                          break;
                      case MetaDataDataType.SingleSelection:
                          if (!String.IsNullOrWhiteSpace(item.Description))
                          {
                              p[mData.Metadata.Name] = Convert.ToInt32(item.Description);
                          }
                          break;                       
                      default:
                          break;
                  }
              }
              var emtyRowList = new List&lt;IDictionary&gt;();
              emtyRowList.Add(p);
  
              viewGrid.DataContext = DataSourceCreator.ToDataSource(emtyRowList).Cast&lt;object&gt;().FirstOrDefault();
              viewGrid.UpdateLayout();
          }
      }
  </code></pre>
  
  <p>I set the <code>DataContext</code> for the complete <code>StackPanel</code>. My <code>TextBox</code> shows the correct Text stored in TempObject.Testprop. My ComboBox` only displays its Items Source but its not showing my already selected item, which Id is stored in TempObject.Nutzen.</p>
  
  <p>Any ideas what I'm missing?</p>
  
  <p>UPDATE:
  I could make it working, binding the SelectedValue to the MetadataData Object instead of its ID value and setting the SelectedValuePath to the ID property name of MetadataData.</p>