Silverlight Combobox loses visual value, but keeps selected value when the page it's on is hidden and reshown
I'm using a Silverlight 4.0 project that utilizes MVVM and we have a combobox that resides on a view and has its values and selected value bound to an observable collection of Organizations and a SelectedOrganization respectively (both values exist on the viewmodel). With our project the page that this control is on can be hidden or shown. The first load everything looks great but when you go to a different control (hide the tab with the control and then go back to it) the value that is currently selected in the combo box looks like it's blank, but when I debug, the selected value is still there.
The visual tree is getting recreated, but I have no idea why the combobox loses the text that should be in the box when the parent page is hidden and then re-shown. All other controls on the page behave correctly (autocompletetextbox, textblocks, textboxes, all of which have data bound to the viewmodel the same way).
Here's how the 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 the viewmodel has the following code for the 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 keep the selected value when I switch parent pages?
The problem is that I declared the ItemsSource AFTER the SelectedItem. Apparently this is a bug in Silverlight 3 and 4. The answer was discussed here Silverlight Combobox and SelectedItem.
Just a quick note on yet a SL3 bug.
I haven't reproduced this bug in a clean environment (since I'm getting quite tiered of reproducing SL ComboBox bug's...), but I experienced an issue with roughly this setup:
- ItemsSource binds to property of type List on Object X.
- SelectedItem binds to property of type String on Object X.
- Object X implements INotifyPropertyChanged.
- SelectedItem is set after ItemsSource in XAML code as the above post instructs.
- ItemsSource is set to TwoWay BindingMode.
Behaviour: When the user TAB's from a textbox into the combobox, the combobox value is 'blanked', while the ViewModel maintains its value. The value is displayed again correctly when the user TAB's out of the ComboBox. Please note that the value is not blanked, if the combobox is merely clicked, or if it tabbed to from another combobox.
Solution: When stepping through the code with a debug'er it seems that SelectedItem is returned before ItemsSource, even though ItemsSource is declared before SelectedItem in the XAML code.
The solution was to change the ItemsSource from TwoWay BindingMode to OneWay BindingMode.
Probably this prevents some events from being fiered behind the scenes.
Br. Morten
Could be helpful if adding to previous post, I noticed that my selectedItem
binding property must contain a conditional that avoid assing null value, because combobox control still wants to reset value once the control is hidden by scroll or whatever.
i.e. :
public string Month{
get {return _month;}
set {
if (value==null)
return;
_month = value;
}
}
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 of
<sdk:DataGrid Name="datagridInvestigation"
AutoGenerateColumns="False" Width="650"
Height="180" ScrollViewer.HorizontalScrollBarVisibility="Auto" >
精彩评论