Search code examples
wpfcommandbinding

wpf record navigation buttons-disable commandbinding


I am using command-bindings to disable buttons in c# visual studio 2010, depending on whether it is the first record or last. I tried to put an argument as below:

    private void CanPreviousPageCommandExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        if (studentsViewSource.View.CurrentPosition == 0)
        {
            e.CanExecute = false;
        }
        else
        {
            e.CanExecute = true;
        }
    }

I get an error "object reference not set to an instance of an object". What object is it talking about or does anyone know how I can correct this?


Solution

  • Probably studentsViewSource or studentsViewSource.View is null so you must modify if statement:

    private void CanPreviousPageCommandExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        if (studentsViewSource != null && studentsViewSource.View != null && studentsViewSource.View.CurrentPosition == 0)
        {
            e.CanExecute = false;
        }
        else
        {
            e.CanExecute = true;
        }
    }