Silverlight Combobox loses its visual value, but retains the selected value when its page is hidden and renamed

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?

+4
source share
4 answers

The problem is that I declared the ItemsSource AFTER the selected item. This seems to be a bug in Silverlight 3 and 4. The answer was discussed here by Silverlight Combobox and SelectedItem .

+1
source

May be useful when adding to the previous post, I noticed that my selectedItem binding property should contain a conditional condition that avoids getting a null value, because the combobox control still wants to have a reset value after the control is hidden by scrolling or what something else.

i.e.

 public string Month{ get {return _month;} set { if (value==null) return; _month = value; } } 
0
source

Just a short note that there is still an SL3 error.

I did not reproduce this error in a clean environment (since I reproduced the SL ComboBox error quite a few times ...), but I had a problem with something like this:

  • ItemsSource is associated with a property of type List in Object X.
  • SelectedItem is associated with a property of type String in Object X.
  • Object X implements INotifyPropertyChanged.
  • SelectedItem is set after ItemsSource in the XAML code, as described above.
  • ItemsSource is configured on TwoWay BindingMode.

Behavior . When a custom TAB from a text field to a combo box, the value of the combo box is โ€œemptyโ€, and the ViewModel retains its value. The value is displayed again correctly when the custom TAB exits the ComboBox. Note that the value does not fade if only the bracket is called, or if it is added to another drop-down sign.

Solution . When navigating through the code with debug'er, it seems that the SelectedItem returns to the ItemsSource, even if the ItemSource is declared before the SelectedItem in the XAML code.

The solution was to change the ItemSource from TwoWay BindingMode to OneWay BindingMode.

This probably prevents some events from slowing down behind the scenes.

Br. Morten

0
source

Use this

 <ScrollViewer Grid.Row="6" Grid.ColumnSpan="4" Height="190"> <sdk:DataGrid Name="datagridInvestigation" AutoGenerateColumns="False" Width="650" MinHeight="180" > </sdk:DataGrid> </ScrollViewer> 

instead

  <sdk:DataGrid Name="datagridInvestigation" AutoGenerateColumns="False" Width="650" Height="180" ScrollViewer.HorizontalScrollBarVisibility="Auto" > 
0
source

All Articles