首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ItemTemplate combox

ItemTemplate combox
EN

Stack Overflow用户
提问于 2011-11-18 09:50:57
回答 2查看 1.2K关注 0票数 0

我想在Windows.UI.Xaml.Controls.ComboxBox中存储两个项目。

  1. 字符串,它将显示在ComboBox
  2. 索引中,该索引不会在CombBox

中显示。

我探索并发现ItemTemplate属性可以做到这一点。有人能给我这个样品吗?

EN

回答 2

Stack Overflow用户

发布于 2011-11-18 09:59:11

你不需要ItemTemplate,你需要的是:

代码语言:javascript
复制
combobox.add(new ListItem("string", "index"); 
票数 1
EN

Stack Overflow用户

发布于 2011-11-21 01:02:50

试试这个:

代码语言:javascript
复制
<UserControl
    x:Class="Application1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="768" 
    d:DesignWidth="1366">
    <Grid 
        x:Name="LayoutRoot" 
        Background="#FF0C0C0C">
        <Rectangle
            x:Name="rect"
            Fill="Black"
            Margin="40,40,0,0" />

        <ComboBox
            Margin="40,40,0,0"
            VerticalAlignment="Top"
            HorizontalAlignment="Left"
            ItemsSource="{Binding Items}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock
                        Text="{Binding Text}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</UserControl>

using System.Collections.Generic;

namespace Application1
{
    partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }
    }

    public class MyItem
    {
        public string Text { get; set; }
        public int Id { get; set; }
    }

    public class MainViewModel
    {
        public List<MyItem> Items { get; set; }

        public MainViewModel()
        {
            this.Items = new List<MyItem>();
            this.Items.Add(new MyItem { Text = "Item 1", Id = 1 });
            this.Items.Add(new MyItem { Text = "Item 2", Id = 2 });
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8180548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档