I need to populate wpf combobox with fixed collection of strings (for example months from january to december).
Get the ComboBox reference. var comboBox = sender as ComboBox; // ... Assign the ItemsSource to the List. comboBox.
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.
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.
Like this?
<ComboBox >
<ComboBoxItem Content="January"></ComboBoxItem>
<ComboBoxItem Content="February"></ComboBoxItem>
<ComboBoxItem Content="March"></ComboBoxItem>
<ComboBoxItem Content="April"></ComboBoxItem>
// .... and so on....
</ComboBox>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With