我正在尝试将ComboBox
数据绑定到strings
列表。到目前为止,我有以下几点:
在我的观点中,我有:
<ComboBox Height="23"
HorizontalAlignment="Left"
Margin="133,180,0,0"
Name="comboBox1"
ItemsSource="{Binding Hours}"
VerticalAlignment="Top"
Width="38" />
在我的ViewModel中我有:
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;
}
}
我希望能够从这些组合框中返回所选项目。我想我差不多了,但只需完成最后一步。
答案 0 :(得分:13)
添加绑定到ComboBox.SelectedItem
的绑定,绑定到ViewModel上的新string
属性
<ComboBox ITemsSource="{Binding Hours}" SelectedItem="{Binding SelectedItem}" />
class ViewModel
{
public string SelectedItem {get; set;}
}