Xaml WinRT组合框SelectedValue为空

Xaml WinRT组合框SelectedValue为空,xaml,windows-phone-8,windows-runtime,win-universal-app,uwp-xaml,Xaml,Windows Phone 8,Windows Runtime,Win Universal App,Uwp Xaml,我创建了一个类,用于创建要添加到combobox的项 public class ComboBoxItemClass { public string Text { get; set; } public object Value { get; set; } public override string ToString() { return Text; } } 对于组合框,我的XAML如下 <TextBlock Text="State

我创建了一个类,用于创建要添加到combobox的项

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;
<ComboBox x:Name="cbState" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding State_Name}"></TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
private void NavigationHelper\u LoadState(对象发送方,LoadStateEventArgs e)
{
列表状态_项=新列表();
列表状态=Location.GetStates();
foreach(州中的州)
{
ComboBoxItemClass item=new ComboBoxItemClass(){Text=s.State\u Name,Value=s.State\u Id};
说明项目。添加(项目);
}
cbState.ItemsSource=状态_项;
cbState.SelectedValue=3;
在emulator中运行时的组合框不显示所选状态。单击该组合框将显示状态列表

调试时,selectedvalue显示为空,尽管为其分配了一个值。
代码的其余部分没有问题,存在一个state_Id=3的状态

我用两种方法解决了这个问题

第一种方法是获取states变量中的states列表。将其分配给ComboBox ItemSource。然后获取State_Id,从相同的states列表中找到该特定状态的索引,并将其分配给所选索引

代码隐藏如下

states = Location.GetStates();

cbState.ItemsSource = states;
cbState.SelectedIndex = states.IndexOf(states.Where(x=>x.State_Id==State_Id).First());
第二种方法如评论部分所建议

 states = Location.GetStates();
 cbState.ItemsSource = states;
 int index=states.IndexOf(states.Where(x=>x.State_Id==State_Id).First());
 cbState.SelectedItem = states[index];
XAML如下所示

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;
<ComboBox x:Name="cbState" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding State_Name}"></TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

您是否尝试设置
SelectedIndex
SelectedValue
的类型为
object
,您需要将其分配给集合中的一个对象以使其正常工作。您选择的索引正在设置,但这不是我想要的。我尝试了cbstate.SelectedValue=mystateobject,但它仍然为nullTry
您说的是
cbstate.SelectedValue=state\u项[3]
不起作用?