Saturday 28 July 2012

Silverlight: Combo Box showing empty value

In Silverlight 5.0, For some Combo Box selected value is becoming empty (or null) .
Only the Selected Value some how becomes null and Combo box looks like following:

image      image

There are couple of work around on this issue but i could not figure out why in the first place Combox selected value is getting null.
Workarounds:
  • First one, i got from this post, By Using ObservableCollection<T> in view code to populate the itemsource (or Datacontext) for the Combo Box.
    In this approach we would need to write code in code behind of xaml (i.e view code)
  • Second One, In Set accessor of “Selected Value” Property  put a check for null value.
public string SelectedValue
{
     get
     { return _svalue; }
     set
     {
         if (value != null)
         {
             _svalue = value;
             onPropertyChanged("SelectedValue"); //INotifyPropertyChange Event
         }
     }
}
Second Approach is easier but it more of a reactive approach.
HTH
Thanks

No comments:

Post a Comment