我创建了一个类来创建要添加到组合框的项目
public class ComboBoxItemClass
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
对于组合框,我的 XAML 如下
<TextBlock Text="State"/>
<ComboBox x:Name="cbState"/>
我在代码隐藏中的 C# 代码如下
private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
List<ComboBoxItemClass> state_items = new List<ComboBoxItemClass>();
List<State> states = Location.GetStates();
foreach(State s in states)
{
ComboBoxItemClass item = new ComboBoxItemClass() { Text = s.State_Name, Value = s.State_Id };
state_items.Add(item);
}
cbState.ItemsSource = state_items;
cbState.SelectedValue = 3;
在模拟器中运行的组合框不显示选中状态。单击它会显示状态列表。
在调试 selectedvalue 时,尽管为其分配了值,但仍显示为 null。其余代码没有问题,存在 State_Id=3 的状态