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

3 posts, 1 answers
  1. Sergi
    Sergi avatar
    4 posts
    Member since:
    Dec 2010

    Posted 11 Jan 2011 Link to this post

    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. Answer
    Pana
    Admin
    Pana avatar
    748 posts

    Posted 17 Jan 2011 Link to this post

    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>>
  3. DevCraft banner
  4. Sergi
    Sergi avatar
    4 posts
    Member since:
    Dec 2010

    Posted 17 Jan 2011 Link to this post

    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
Back to Top