Reputation: 113
Expected Behavior
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.
Upvotes: 0
Views: 3124
Reputation: 292425
You don't need the [0]
part (it's a bool, not a collection), and the binding doesn't need to be TwoWay
:
IsEnabled="{Binding Path=EndDateEnabled}"
You also need to implement INotifyPropertyChanged
in your ViewModel, and raise the PropertyChanged
event for the EndDateEnabled
property (and for all properties that your view is bound to):
private bool _endDateEnabled;
public bool EndDateEnabled
{
get { return _endDateEnabled; }
set
{
if (value != _endDateEnabled)
{
_endDateEnabled = value;
OnPropertyChanged("EndDateEnabled");
}
}
}
Note that EndDateEnabled
can also be a computed property with only a getter:
public bool EndDateEnabled
{
get { return _dateModeValue == DateMode.CustomDateRange; }
}
In this case you need to call OnPropertyChanged("EndDateEnabled")
in the DateModeValue
setter, so that the binding is refreshed.
Upvotes: 5
Reputation: 1535
There are 2 problems that I see. First, your binding to EndDateEnabled should look more like:
IsEnabled="{Binding Path=EndDateEnabled}"
The [0] isn't valid or necessary since the target of the binding is just a plan bool property. The TwoWay shouldn't apply here, I don't think, but it doesn't hurt to have it.
The backing ViewModel should implement INotifyPropertyChanged in order to notify the UI that the property has changed. The code for the EndDateEnabled property might end up looking something like:
private bool endDateEnabled = false;
public bool EndDateEnabled
{
get { return endDateEnabled; }
set
{
if (endDateEnabled != value)
{
endDateEnabled = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("EndDateEnabled"));
}
}
}
I did that from memory so it may not be perfect but it is the general idea.
Upvotes: 0