如何在 xaml 中填充 wpf 组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14213398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:56:56  来源:igfitidea点击:

How to populate wpf combobox in xaml

wpfcombobox

提问by synergetic

I need to populate wpf combobox with fixed collection of strings (for example months from january to december).

我需要用固定的字符串集合(例如从一月到十二月的几个月)填充 wpf 组合框。

回答by synergetic

In namespace add declaration:

在命名空间中添加声明:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

then add combobox where appropriate:

然后在适当的地方添加组合框:

<ComboBox>
  <sys:String>January</sys:String>
  <sys:String>February</sys:String>
  <sys:String>March</sys:String>
                ...
  <sys:String>December</sys:String>
</ComboBox>

回答by CB.

Like this?

像这样?

<ComboBox > 

<ComboBoxItem Content="January"></ComboBoxItem> 

<ComboBoxItem Content="February"></ComboBoxItem> 

<ComboBoxItem Content="March"></ComboBoxItem> 

<ComboBoxItem Content="April"></ComboBoxItem> 

// .... and so on....

</ComboBox>

回答by Virus

You should bind the ItemsSourceof the combo box to the property of the List<string>.

您应该将ItemsSource组合框的绑定到List<string>.