tags:

views:

1184

answers:

4

i have a mvvm app that the main window is a tab control. i use the itemssource to bind items to the combo box, everything works fine until i go to another tab and for some reason the selected item of the combo box getting the null value, any ideas ?

the binding is twoway updatesource onpropertychanged and the property is type of observablecollection

A: 

I have an mvvm app with almost exactly the same scenario. The main window has a tab control. There is a tab containing a combobox. The combobox itemsource is bound to an IList (in the view model) and the Selected value is bound to a property in the view model implementing INotifyPropertyChanged.

<ComboBox ItemsSource="{Binding AllowedJudges}"  
                  SelectedValue="{Binding SelectedJudge, UpdateSourceTrigger=PropertyChanged}"  >

When selecting another tab, the view model's property bound to the SelectedValue mysteriously gets set to null. I'm able to handle it by not allowing the SelectedValue-bound property to be set to null:

 public Judge SelectedJudge
  {
     get { return selectedJudge; }
     set
     {
        if(selectedJudge==value || value==null) return;
        selectedJudge = value;
        OnPropertyChanged("SelectedJudge");
        updateViewData();
     }
  }

However, it's not clear to me why a tab pane becoming invisible implies a value in a combobox there becomes deselected....

Keith Klein
this is my problem two
Chen Kinnrot
Is there any reason why you use SelectedValue and not SelectedItem?Not necessarily a mistake, as long as you know what you are doing, just irritating me.Seeing the action occurring in updateViewData might help solving your issue.
Simpzon
A: 

If for some Reason the BindingSource of the ItemsSource does no longer contain the SeletedItem (because it's re-initialized or whatever), then the SelectedItem can be reset to default, i.e. null. From your example I can't tell why this should happen, but that maybe because you just missed adding some more environmental code.

Simpzon
A: 

It`s may be problem of your viewmodel ierarchy. For example, if both ends of your Binding are dependency properties and ownertype property is not tied to a particular class (for example, set the parent class), this dependency property will be used by all the inheritors together. Bad design

madcyree
+1  A: 

I had same problem before. And the solution is that make sure Itemsource attribute of comboBox in XAML has not been declared before SelectedValue attribute. It should work then.

punjabi_ikon
I have seen XAMl above. Move Itemsource attribute after selectedvalue attribute. I shall work
punjabi_ikon