WPF Combo Box Data Composition

I am trying to bind a ComboBox to a strings list. So far I have the following:

In my view, I have:

 <ComboBox Height="23" HorizontalAlignment="Left" Margin="133,180,0,0" Name="comboBox1" ItemsSource="{Binding Hours}" VerticalAlignment="Top" Width="38" /> 

And in my ViewModel model, I have:

 private List<string> tripTimeHours = new List<string>(); private List<string> tripTimeMinutes = new List<string>(); public CreateTripViewModel() { TripName = new DataWrapper<string>(this, tripNameChangeArgs); TripName.IsEditable = true; setObjects(); CreateTripFiredCommand = new SimpleCommand<object, EventToCommandArgs>(ExecuteCreateTripCommand); } private void setObjects() { for (int i = 0; i < 24; i++) { tripTimeHours.Add(i.ToString()); } for (int i = 0; i < 60; i++) { tripTimeMinutes.Add(i.ToString()); } } public List<string> Hours { get { return tripTimeHours; } } public List<string> Minutes { get { return tripTimeMinutes; } } 

What I want to do is return the selected item from these lists. I think I'm almost there, but just need to complete the last step.

+4
source share
1 answer

Add a binding to ComboBox.SelectedItem , which is bound to the new string property on your ViewModel

 <ComboBox ITemsSource="{Binding Hours}" SelectedItem="{Binding SelectedItem}" /> class ViewModel { public string SelectedItem {get; set;} } 
+13
source

All Articles