我有一个组合框,需要显示所有美国州。我使用ItemsSource进行了数据绑定,并且还在ViewModel中设置了SelectedItem属性。运行代码时,我看到正在设置SelectedItem。但这并没有显示在ComboBox下拉列表中。它没有设置为任何值,但是当我单击下拉列表时会显示所有状态。不知道我在做什么错。
这就是我的看法:
<ComboBox x:Name="Text10" Height="20" Grid.Row="9" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding StateList}" SelectedItem="{Binding SelectedState,Mode=TwoWay}" DisplayMemberPath="Ix_type" Margin="5" VerticalAlignment="Center"/>
和ViewModel:
private ObservableCollection<Indexing_Type> stateList = new ObservableCollection<Indexing_Type>();
public ObservableCollection<Indexing_Type> StateList
{
get { return stateList; }
set
{
if (value != stateList)
{
stateList = value;
OnPropertyChanged("StateList");
}
}
}
private Indexing_Type selectedState;
public Indexing_Type SelectedState
{
get { return selectedState; }
set
{
if (value != selectedState)
{
selectedState = value;
OnPropertyChanged("SelectedState");
}
}
}
填充组合框:
var states = GetStates()
foreach(var item in states)
{
StateList.Add(new Indexing_Type
{
Id = item.Id,
Ix_type = item.Ix_type
});
}
Indexing_Type ixType = new Indexing_Type();
ixType.Id = 5;
ixType.Ix_type ="CA";
SelectedState = ixType;
Indexing_Type模型:
public class Indexing_Type
{
public int Id { get; set; }
public string Ix_type { get; set; }
}
谢谢 任何帮助表示赞赏。