This is a migrated thread and some comments may be shown as answers.

MVVM, IsSelected property isn't taken into account at first

2 Answers 72 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Sergi
Top achievements
Rank 1
Sergi asked on 11 Jan 2011, 04:19 PM
Hello,

I'm using a RadComboBox binded to an ObservableCollection of my own ViewModels. On my ViewModels I have an IsSelected property binded to the IsSelected property of the RadComboBoxItems. To achieve this, since you can't set a binding in a setter in Silverlight, I've had to inherit from RadComboBox and add this override:

protected override DependencyObject GetContainerForItemOverride()
{
    RadComboBoxItem container = new RadComboBoxItem();
    Binding selectedBinding = new Binding("IsSelected");
    selectedBinding.Mode = BindingMode.TwoWay;
    container.SetBinding(RadComboBoxItem.IsSelectedProperty, selectedBinding);
 
    return container;
}

Everything works well except for one single problem. I need to set one of my objects as selected when first getting the items (which I get after loading the control since I get them from a webservice). Basically, when I receive the data and populate the collection I set the first ViewModel's IsSelected property to true. Well, the combobox doesn't select anything. It doesn't matter whether the combobox is editable, but I need it to be editable if it's important.
Once the user selects an item everything works well.
My guess would be that the control virtualizes the items and it doesn't create them until the user opens it for the first time. I've tried setting VirtualizingStackPanel.IsVirtualizing to false to no avail - in fact, when I do that Silverlight encounters an error and doesn't render my view.

How could I fix this problem? By the way, this combobox is in a template so I don't have code-behind. Besides, I'd prefer not to use code-behind if possible, I prefer using MVVM.

2 Answers, 1 is accepted

Sort by
0
Accepted
Pana
Telerik team
answered on 17 Jan 2011, 09:20 AM
Hello,

The contents of the Popup in the RadComboBox are collapsed until opened. You have figured that right. It does not matter if the combo is virtualizing or not items will not be generated until you open the combo for first time. And if it is virtualizing it will not generate all items when you open it.

It would be best to create a "SelectedItem" property on your view model, bind the SelectedItem of the combo to the SelectedItem of your view model and set the "SelectedItem" when you load the items. I don't know how this will fit in your design.

Best wishes,
Pana
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Sergi
Top achievements
Rank 1
answered on 17 Jan 2011, 10:28 AM
Thanks Pana, it works. I've had to do a little bit of extra work (managing the current selection myself, unselecting the item that is no longer selected) but other than that it works well.

-Sergi
Tags
ComboBox
Asked by
Sergi
Top achievements
Rank 1
Answers by
Pana
Telerik team
Sergi
Top achievements
Rank 1
Share this question
or