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

Binding Issue

1 Answer 65 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
lori guymon
Top achievements
Rank 1
lori guymon asked on 27 Sep 2010, 05:21 PM
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!

1 Answer, 1 is accepted

Sort by
0
lori guymon
Top achievements
Rank 1
answered on 27 Sep 2010, 05:38 PM
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}); }
}

Tags
ComboBox
Asked by
lori guymon
Top achievements
Rank 1
Answers by
lori guymon
Top achievements
Rank 1
Share this question
or