2009-11-24 153 views
54

我以前说过,我再说一遍,为WPF最简单的例子也是最难找到的网页:)WPF - 添加静态项目组合框

我有一个组合上框,我需要显示,但它不需要数据绑定或其他任何东西,内容是静态的。如何使用XAML将静态项目列表添加到我的组合框中?

回答

101

下面是从MSDN和链接的代码 - Article Link,你应该检查出更多的细节。

<ComboBox Text="Is not open"> 
    <ComboBoxItem Name="cbi1">Item1</ComboBoxItem> 
    <ComboBoxItem Name="cbi2">Item2</ComboBoxItem> 
    <ComboBoxItem Name="cbi3">Item3</ComboBoxItem> 
</ComboBox> 
19

像这样:

<ComboBox Text="MyCombo"> 
<ComboBoxItem Name="cbi1">Item1</ComboBoxItem> 
<ComboBoxItem Name="cbi2">Item2</ComboBoxItem> 
<ComboBoxItem Name="cbi3">Item3</ComboBoxItem> 
</ComboBox> 
+1

我很感激。 – 2009-11-24 18:15:46

6

感谢您的帮助。这只是帮助我。侧面说明,你也可以添加物品代码:

cboWhatever.Items.Add("SomeItem"); 

此外,添加你控制显示/有价值的东西,(几乎断然需要在我的经验),你可以这样做。我发现了一个很好的参考计算器这里:

Key Value Pair Combobox in WPF

点心机代码将是这样的:

ComboBox cboSomething = new ComboBox(); 
cboSomething.DisplayMemberPath = "Key"; 
cboSomething.SelectedValuePath = "Value"; 
cboSomething.Items.Add(new KeyValuePair<string, string>("Something", "WhyNot")); 
cboSomething.Items.Add(new KeyValuePair<string, string>("Deus", "Why")); 
cboSomething.Items.Add(new KeyValuePair<string, string>("Flirptidee", "Stuff")); 
cboSomething.Items.Add(new KeyValuePair<string, string>("Fernum", "Blictor")); 
0
<ComboBox Text="Something"> 
      <ComboBoxItem Content="Item1"></ComboBoxItem > 
      <ComboBoxItem Content="Item2"></ComboBoxItem > 
      <ComboBoxItem Content="Item3"></ComboBoxItem > 
</ComboBox> 
+1

请加上信息,为什么你的解决方案可能有助于OP – milo526 2017-01-06 13:09:58