0

Windows.UI.Xaml.Controls.ComboxBox に 2 つのアイテムを格納したかったのです。

  1. ComboBox に表示される文字列

  2. CombBox に表示されないインデックス

私が調べたところ、ItemTemplate プロパティがこれを実行できることがわかりました。誰かが私にこれのサンプルを提供してもらえますか.

4

2 に答える 2

1

は必要ItemTemplateありません。必要なものは次のようになります。

combobox.add(new ListItem("string", "index"); 
于 2011-11-18T09:59:11.763 に答える
1

これを試して:

<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 });
        }
    }
}
于 2011-11-21T01:02:50.270 に答える