【发布时间】:2010-12-09 00:23:37
【问题描述】:
我想在后面的代码中用键/值数据填充 ComboBox,我有这个:
XAML:
<Window x:Class="TestCombo234.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestCombo234"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider x:Key="Choices" ObjectType="{x:Type local:CollectionData}" MethodName="GetChoices"/>
</Window.Resources>
<StackPanel HorizontalAlignment="Left">
<ComboBox ItemsSource="{Binding Source={StaticResource Choices}}"/>
</StackPanel>
</Window>
代码隐藏:
using System.Windows;
using System.Collections.Generic;
namespace TestCombo234
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public static class CollectionData
{
public static Dictionary<int, string> GetChoices()
{
Dictionary<int, string> choices = new Dictionary<int, string>();
choices.Add(1, "monthly");
choices.Add(2, "quarterly");
choices.Add(3, "biannually");
choices.Add(4, "yearly");
return choices;
}
}
}
我必须改变什么,以便键是 int,值是字符串?
【问题讨论】:
-
看起来您上面问题中的图像已损坏(现在是showing an ad instead)。您能否将图像重新上传到 stack.imgur,或编辑您的问题以将其删除?
标签: c# wpf xaml data-binding combobox