0

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?

nos9
  • 601
  • 2
  • 6
  • 16
  • We can not bind `ChangeGameDay` Command to `DateChanged` event, we should be able to use `EventTriggerBehavior`. There are a hand full of events that are not supported by the behavior framework. The `DateChanged` is not supported. – Jayden Sep 22 '17 at 03:00
  • This is not a typical NullRef exception as Jayden explained. Question should be reopened and answered. – Bart Sep 22 '17 at 06:11
  • @JaydenGu-MSFT Thanks for clarifying that. I've not heard of EventTriggers before, but I'll look into it and see if I can figure it out. – nos9 Sep 22 '17 at 14:21

0 Answers0