Expected Behavior
- There are ComboBox and DatePicker.
- On Selection changed of the combo box, depending on the combobox selection the DatePicker should be enabled or disabled.
How I am trying to implement
XAML code for Date Interval and Date Picker
<ComboBox ItemsSource="{Binding Source={StaticResource viewByInterval}}"
SelectedValuePath="Value"
SelectedItem="{Binding IntervalMode,Mode=TwoWay}" />
<DatePicker SelectedDate="{Binding EndDate,Mode=TwoWay}"
IsEnabled="{Binding Path=EndDateEnabled[0],Mode=TwoWay}">
View Model code for changing the EndDateEnabled
public bool EndDateEnabled { get; set; }
public DateMode IntervalMode
{
get
{
return _dateModeValue;
}
set
{
_dateModeValue = value;
EndDateEnabled = (value == DateMode.CustomDateRange);
}
}
I am unable to achieve the functionality.
Please advice.
You don’t need the
[0]part (it’s a bool, not a collection), and the binding doesn’t need to beTwoWay:You also need to implement
INotifyPropertyChangedin your ViewModel, and raise thePropertyChangedevent for theEndDateEnabledproperty (and for all properties that your view is bound to):Note that
EndDateEnabledcan also be a computed property with only a getter:In this case you need to call
OnPropertyChanged("EndDateEnabled")in theDateModeValuesetter, so that the binding is refreshed.