How to insert years series in ComboBoxItem in WPF

It is very hard to put a year range from past to present or any others by typing one by one in ComboBoxItem of ComboBox. We can easily put the year inside a ComboBox by a simple loop, which will help to load our expected year range inside a ComboBox .

Not only years, we can load other series of numbers like months, dates inside a ComboBox, according to our requirement by applying this technique as well.

OK. It is time to see the procedure.

  • First of all take a ComboBox.
  • Put a name of the ComboBox e.g. Name=”myComboBox”.
  • Now open the .cs page and write the given for loop inside MainWindow().
for (int i = 1950; i <=DateTime.UtcNow.Year; i++)
ComboBoxItem item = new ComboBoxItem();
item.Content = i;

Here we select the starting year as 1950 and the end date as the current year.

Now save and debug the project and you will see the ComboBoxItem like the following image.

ComboBoxItem With Years.jpg

ComboBoxItem With Years

Try it yourself with some numbers.

Download the sample code.