I am using a Silverlight 4.0 project that uses MVVM, and we have a combo box that is in the view and has its values โโand the selected value, tied to the observed collection of organizations and SelectedOrganization respectively (both values โโexist in the viewmodel). In our project, the page on which this control is located can be hidden or shown. The first load looks great, but when you switch to another control (hide the tab with the control and then return to it), the value that is currently selected in the combo box looks empty, but when I debug, the selected value still exists.
The visual tree is recreated, but I have no idea why the combobox loses the text that should be in the field when the parent page is hidden and then re-displayed. All other controls on the page behave correctly (autocompletetextbox, text blocks, text fields, all of which have data attached to the viewmodel in the same way).
Here, as combobox is declared:
<ComboBox SelectedItem="{Binding SelectedOrganization, Mode=TwoWay}" ItemsSource="{Binding Organizations}" DisplayMemberPath="Name" Margin="5,0" MinWidth="100" />
The class for the organization is here:
[DataContract] public class Organization { [DataMember] public Guid OrganizationID { get; set; } [DataMember] public string Name { get; set; } }
and in view mode there is the following code for bindings:
public Organization SelectedOrganization { get { return (Organization)GetValue("SelectedOrganization"); } set { SetValue("SelectedOrganization", value); } } public ObservableCollection<Organization> Organizations { get { return (ObservableCollection<Organization>)GetValue("Organizations"); } set { SetValue("Organizations", value); } }
What do I need to do to save the selected value when switching parent pages?
source share