Cannot get the UI to update when setting SelectedItem from code

4 posts, 0 answers
  1. Stephen
    Stephen avatar
    43 posts
    Member since:
    Apr 2012

    Posted 18 Apr 2013 Link to this post

    We are using MVVM.  No matter what I do, I cannot get the UI to change when I set the SelectedItem property via the ViewModel.  I have tried SelectedItem, SelectedIndex, Text and nothing works for me.

    Here is my xaml:
    <telerik:RadComboBox Width="135"
                         Height="25"
                         Margin="10,0,0,0"
                         DisplayMemberPath="Description"
                         IsEnabled="{Binding GlobalIsEnabled}"
                         ItemsSource="{Binding ComplexionItemsSource}"
                         SelectedItem="{Binding ComplexionSelectedItem, Mode=TwoWay}"
                         Style="{StaticResource RadComboBoxStandard}" />

    Here is my ItemsSource property which is bound in my xaml:
    private ObservableCollection<Complexion> _ComplexionItemsSource = new ObservableCollection<Complexion>();
     
    public ObservableCollection<Complexion> ComplexionItemsSource
    {
        get
        {
            return _ComplexionItemsSource;
        }
        set
        {
            _ComplexionItemsSource = value;
            RaisePropertyChanged("ComplexionItemsSource");
        }
    }

    Here is my SelectedItem property which is bound in my xaml:
    private Complexion _ComplexionSelectedItem;
     
    public Complexion ComplexionSelectedItem
    {
        get
        {
            return _ComplexionSelectedItem;
        }
        set
        {
            _ComplexionSelectedItem = value;
            RaisePropertyChanged("ComplexionSelectedItem");
        }
    }

    Here is my Web Service completed method, where I populate the ItemsSource (which works) and then I try to set the SelectedItem (which doesn't update the UI, but my SelectedItem raise property event does fire)
    private void ComplexionSelectCompletedReturn(object sender, ClientComplexionSelectCompletedArgs e)
    {
        if (e.ClientComplexionSelectCompletedResult != null)
        {
            foreach (var _Item in e.ClientComplexionSelectCompletedResult)
            {
                ComplexionItemsSource.Add(_Item);
            }
     
            if (ComplexionItemsSource.Count > 0)
            {
                for (var i = 0; i < ComplexionItemsSource.Count(); i++)
                {
                    if (ComplexionItemsSource[i].IsDefault)
                    {
                        ComplexionSelectedItem = ComplexionItemsSource[i];
                    }
                }
            }
        }
    }

    This problem is driving me absolutely bonkers!  I also tried setting up a binding to SelectedIndex, but that doesn't work either.  Based on what I have read, I have this set up right.  The combo gets the collection and I can change the values via the UI.  I just cannot change it via the ViewModel.
  2. Stephen
    Stephen avatar
    43 posts
    Member since:
    Apr 2012

    Posted 19 Apr 2013 Link to this post

    Also, just an FYI, I have 4 collections which were populated by enumerations and I can set the SelectedItem property just fine and it updates the UI.

    I also thought I should attach my Raise Property method and event, just in case it is of importance to my problem:

    Here is the event:
    public new event PropertyChangedEventHandler PropertyChanged;

    And the method:
    public void RaisePropertyChanged(string pPropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(pPropertyName));
        }
    }

    I could really use some help on this as it is costing me a ton of time, trying everything I come across to try and resolve this issue.
  3. DevCraft banner
  4. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 23 Apr 2013 Link to this post

    Hi Stephen,

    Could you please try setting SelectedValue instead of SelectedItem? This works as expected in our test project.  However, we will research further why SelectedItem is not set correctly.

    Let us know whether this helps.

    All the best,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  5. Stephen
    Stephen avatar
    43 posts
    Member since:
    Apr 2012

    Posted 23 Apr 2013 Link to this post

    We finally got it figured out.  The issue was that my view model inherited from a base view model which implemented a property changed method.  I had a property changed event in my view model as well because I didn't realize this base view model class had one in there.  Once I removed the property changed event from my view model, everything worked just fine.

    The reason why it was so hard for me to pin this down is because the collections for the combos populated just fine, when I had my property changed event in my view model.  So, it was odd to me that I could do whatever I wanted to the collection and it was reflected in the UI, but I couldn't set values from the view model.

    So, I am all set on this.  I have been able to get my UI to work now, regardless of which way I set my value.

    Thank you for your response.
Back to Top