<ComboBox Grid.Row="1" Grid.Column="0" Width="Auto" Name="cmbBudgetYear">
<ComboBoxItem Content="2009" />
<ComboBoxItem Content="2010" />
<ComboBoxItem Content="2011" />
<ComboBoxItem Content="2012" />
</ComboBox>
How do I set the selected item to the current year in the code behind?
Something like…
cmbBudgetYear.SelectedItem = cmbBudgetYear.Items(
get the item with the Now.Year.ToString)
There exist many ways to do this but for your example, I would change the ComboBox-Tag as follows:
I added the attribute-defition
SelectedValuePath="Content". After that you can set the value with a corresponding string, e.g.:Take care that the value must be a string. For your example, use
An additional idea
If you use the code-behind anyway, would it be a possibility to fill the combobox with integers. Someting like:
..then you can select the values extremly simple like
… and you would have also other advantages.