Binding Issue

2 posts, 0 answers
  1. lori guymon
    lori guymon avatar
    23 posts
    Member since:
    May 2004

    Posted 27 Sep 2010 Link to this post

    Hello,
    I have a very simple combobox that has a source of type ObservableCollection<String>.  The combobox is bound to a property of an entity object (Event.WaveDuration) that is of type int.  Everything works great but when I open the form to edit the entity, I cannot figure out how to get the combobox to initially display the value that is stored in the Event.WaveDuration property.  The combobox comes up blank (though the dropdown displays the collection just fine) and if I select a different value from the list, it is saved properly to the database.  So I know the binding is working since the save is working properly.

    I have tried many things (thinking I did not need to use the SelectedItem property in xaml but since that didn't work I assumed I needed it to convert the int to a string).  I have tried to use ObservableCollection<int> as well with the same results. 

    So my issue is trying to display the value in Event.WaveDuration when the form initially opens.. 

    Here is my code thus far:
    XAML:

    <telerikInput:RadComboBox 
       ItemsSource="{Binding WaveDurations}"                                                                                                     
       SelectedItem="{Binding SelectedWaveDuration,  Mode=TwoWay}"
       SelectedValue="{Binding Event.WaveDuration, Mode=TwoWay}" >            
    </telerikInput:RadComboBox>

    MVVM:
    public ObservableCollection<string> WaveDurations {
        get {
            if (_waveDurations == null) {
                _waveDurations = new ObservableCollection<string> {"10", "15", "20", "30"};
                SelectedWaveDuration = this.Event.WaveDuration.ToString();
            }
            return _waveDurations;
        }
    }
      
    public string SelectedWaveDuration
    {
        get { return _selectedWaveDuration; }
        set {
            _selectedWaveDuration = value;
            this.OnPropertyChanged(m => m.SelectedWaveDuration);
        }
    }


    Thank you in advance!
  2. lori guymon
    lori guymon avatar
    23 posts
    Member since:
    May 2004

    Posted 27 Sep 2010 Link to this post

    Please disregard as I figured it out.  It was even more simple than I thought.  For some reason, the SelectedValue is not needed. Here is all that is needed:

    XAML:
    <telerikInput:RadComboBox
            ItemsSource="{Binding WaveDurations}" 
            SelectedItem="{Binding Event.WaveDuration, Mode=TwoWay}" >       
    </telerikInput:RadComboBox>

    MVVM:
    public ObservableCollection<int> WaveDurations {
        get { return _waveDurations ?? (_waveDurations = new ObservableCollection<int> {10, 15, 20, 30}); }
    }

Back to Top