【问题标题】:Finding the Index of an item in a combo box XAML/C#/WinRT在组合框中查找项目的索引 XAML/C#/WinRT
【发布时间】:2014-09-17 06:23:14
【问题描述】:

所以我做了很多研究并尝试了很多人的解决方案,但似乎没有任何效果,我不知道为什么。 我正在尝试在我的 ComboBox 中查找项目的索引:

string Index = programType.Items.IndexOf("Lose Weight").ToString();

索引始终为“-1”,即使肯定有一个项目具有“减肥”的内容/标签:

<ComboBox x:Name="programType" Header="Desired goal:" HorizontalAlignment="Left" Grid.Row="5" Grid.Column="1" Margin="30,0,0,0" Grid.ColumnSpan="2" VerticalAlignment="Top" Width="200" PlaceholderText="Desired Goal">
    <ComboBoxItem Content="Lose Weight" Tag="Lose Weight" IsSelected="True" />
    <ComboBoxItem Content="Get Healthier" Tag="Get Healthier"/>
    <ComboBoxItem Content="Get Stronger" Tag="Get Stronger"/>
    <ComboBoxItem Content="Cardio Booster" Tag="Cardio Booster"/>
</ComboBox>

据我所知,它应该可以工作?我一定做错了什么,有什么提示吗?谢谢!

【问题讨论】:

    标签: c# xaml winrt-xaml win-universal-app


    【解决方案1】:

    当您以这种方式添加项目时,Items 属性包含 ComboBoxItem 类型的对象。使用以下代码获取字符串列表:

    int index = programType.Items
        .Cast<ComboBoxItem>()
        .Select(c => (string)c.Content)
        .ToList()
        .IndexOf("Lose Weight");
    

    【讨论】:

    • 完美运行,非常感谢!我不知道为什么我在其他任何地方都找不到这个答案。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2012-06-23
    相关资源
    最近更新 更多