I'm setting the values in three combo boxes:Year, Month and Day. The year combo box has data stored in ObservableCollection<Year> YearsList in descending order and can be set with SelectedIndex="0" to always display the current year when the page renders:
View:
<ComboBox Name="cboYear"
ItemsSource="{Binding YearsList, Mode=OneTime}"
DisplayMemberPath="year"
SelectedIndex="0"
SelectedItem="{Binding SelectedYear, Mode=TwoWay}" />
ViewModel:
public ObservableCollection<Year> YearsList { get; set; }
private Year _selectedYear;
public Year SelectedYear
{
get
{
return _selectedYear;
}
set
{
_selectedYear = value;
RaisePropertyChanged("SelectedYear");
}
}
@Will suggests to use a SelectedIndex/SelectedItem approach but doing so with cboYear (i.e., removing SelectedIndex="0") renders a blank default value for the Year combo box. So using this approach, do I need another method as @Clemens and @Almulo suggest to render a default value or can such logic be included in the SelectedYear property?