title: Silverlight 4 ComboBox - Binding to Nullable data (tried TargetNullValue but not working as expected)
body: <p>(Please note - I am a Silverlight beginner and am looking for the simplest solution here, e.g. that doesn't involve writing/installing a replacement for the ComboBox control!)</p>
  
  <p>This is an issue with a Silverlight 4 application that uses the View Model (MVVM) approach. I have a simple form for editing a "Product" object. Product has a CategoryID property which is nullable (int?). A ComboBox is used to view and set the CategoryID - this is bound to an ObservableCollection of Categories. Product also has number of non-nullable properties bound to TextBoxes.</p>
  
  <p>I want the user to see "N/A" in the ComboBox for a product with no category, and to be use this "N/A" option to set CategoryID to null. So, I manually added  a Category object with CategoryID=0 and CategoryName="N/A" to the collection; then I set TargetNullValue=0 in the SelectedValue Binding of the ComboBox. My thinking was - when the ComboBox SelectedValue was bound to a null CategoryID it would substitute zero, and therefore select the "N/A" option.</p>
  
  <p>When editing a Product with a non-null CategoryID, everything works. However when a null CategoryID is found, two problems occur:</p>
  
  <ol>
  <li><p>No option is selected in the ComboBox (its blank)</p></li>
  <li><p>The ComboBox binding seems broken from this point onwards - any Product I subsequently edit (incl. ones with a non-null CategoryID) have nothing selected in the ComboBox (its still populated with all categories - just no selected item).</p></li>
  </ol>
  
  <p>I've seen reports of problem #2 (<a href="http://betaforums.silverlight.net/forums/p/173676/510666.aspx" rel="nofollow">here</a>, <a href="http://forums.lhotka.net/forums/p/9786/45971.aspx" rel="nofollow">here</a>) but I was under the impression that #1 should have worked.</p>
  
  <p>What am I missing to get the "N/A" option to be selected?</p>
  
  <p>XAML for ComboBox:</p>
  
  <pre><code>&lt;ComboBox x:Name="cboCategory" ItemsSource="{Binding colCategories, Mode=OneWay}" SelectedValuePath="CategoryID" DisplayMemberPath="CategoryName" SelectedValue="{Binding CurrentProduct.CategoryID, Mode=TwoWay, TargetNullValue=0}" Height="24" Width="344"&gt;&lt;/ComboBox&gt;
  </code></pre>