【发布时间】:2016-04-04 08:21:40
【问题描述】:
我创建了一个类来创建要添加到组合框的项目
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的状态
【问题讨论】:
-
您是否尝试设置
SelectedIndex?SelectedValue属于object类型,您需要将其指定为集合中的对象之一才能使其工作。 -
你选择的索引正在设置,但这不是我想要的
-
我试过 cbstate.SelectedValue = mystateobject 但它仍然为空
-
试试
<ComboBox ... SelectedValuePath="Value" /> -
你是说
cbstate.SelectedValue = state_items[3]不起作用?
标签: xaml windows-phone-8 windows-runtime win-universal-app uwp-xaml