Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate wpf combobox in xaml [closed]

Tags:

combobox

wpf

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

like image 528
synergetic Avatar asked Jan 08 '13 10:01

synergetic


People also ask

How do I populate a ComboBox in WPF?

Get the ComboBox reference. var comboBox = sender as ComboBox; // ... Assign the ItemsSource to the List. comboBox.

What happens when we select a value from a ComboBox in WPF?

When you select an item, it will be displayed on the textbox. We recommend that you execute the above example code and try some other properties and events of the combobox control.

How do I add a ComboBox item in WPF?

On button click event handler, we add the content of TextBox to the ComboBox by calling ComboBox. Items. Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ComboBox.


2 Answers

Like this?

<ComboBox > 

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

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

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

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

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

</ComboBox>
like image 137
CB. Avatar answered Oct 08 '22 10:10

CB.


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>
like image 21
synergetic Avatar answered Oct 08 '22 10:10

synergetic