I'm working on a UWP app using MVVM light and I'm running into an issue when trying to bind a relay command from my view model to the DateChanged event of a DatePicker control.
View
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<DatePicker Date="{Binding GameDay}" DateChanged="{Binding ChangeGameDay}"/>
</Grid>
ViewModel
public class ScheduleViewModel : ViewModelBase
{
private readonly INavigationService _navigationService;
public ScheduleViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
}
private DateTime gameDay;
public DateTime GameDay
{
get { return gameDay; }
set
{
gameDay = value;
RaisePropertyChanged(() => GameDay);
}
}
private RelayCommand changeGameDay;
public RelayCommand ChangeGameDay
{
get
{
return changeGameDay
?? (changeGameDay = new RelayCommand(
() =>
{
// TODO: Change game day data
}));
}
}
}
When attempting to compile the app I get an object reference error
Object reference not set to an instance of an object C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\WindowsXaml\v15.0\8.2\Microsoft.Windows.UI.Xaml.Common.targets 266
If I remove the DateChanged event handler from the view, this error goes away. Is there a different way I'm supposed to bind to the event?