C# ItemTemplateCombox
我想在Windows.UI.Xaml.Controls.ComboxBox中存储两个项目C# ItemTemplateCombox,c#,xaml,windows-runtime,C#,Xaml,Windows Runtime,我想在Windows.UI.Xaml.Controls.ComboxBox中存储两个项目 将显示在组合框中的字符串 不会显示在组合框中的索引 我探索并发现ItemTemplate属性可以做到这一点。 有人能为我提供这个示例吗。您不需要ItemTemplate,您需要的是: combobox.add(new ListItem("string", "index"); 试试这个: <UserControl x:Class="Application1.MainPage" xml
有人能为我提供这个示例吗。您不需要
ItemTemplate
,您需要的是:
combobox.add(new ListItem("string", "index");
试试这个:
<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 });
}
}
}
使用System.Collections.Generic;
命名空间应用程序1
{
部分类主页
{
公共主页()
{
初始化组件();
this.DataContext=新的MainViewModel();
}
}
公共类MyItem
{
公共字符串文本{get;set;}
公共int Id{get;set;}
}
公共类主视图模型
{
公共列表项{get;set;}
公共主视图模型()
{
this.Items=新列表();
添加(新的MyItem{Text=“Item 1”,Id=1});
添加(新的MyItem{Text=“Item 2”,Id=2});
}
}
}
如果我这样做,字符串将显示在组合框中??组合框中只显示一个值,您可以访问其他值,如((ListItem)(cmbWH.SelectedItem))。ShortName/LongName
抱歉,我忘了提到它的新组合框。我正在使用WinRT comboBox。更新了问题。