Custom commands

2 posts, 1 answers
  1. Brandon
    Brandon avatar
    18 posts
    Member since:
    Nov 2009

    Posted 12 Oct 2015 Link to this post

    I've created a new command in my ViewModel of the scheduleview that works properly in a context menu associated with the scheduleview, however when I try and place this command on a button outside the scheduleview control it doesn't trigger that an appointment has been selected or not. 

    I can make the CanExecute always return true and then it will fire the event properly when clicked, however I need it to disable/enable when an appointment is selected and it has specific values set in the appointment.  The edit button triggers the selection changed ok when outside the schedule view.  How do I get this same functionality for my command/button?

    public DelegateCommand StartVisitCommand { get { return this.startVisitCommand; } set { this.startVisitCommand = value; } }

    <telerik:RadButton Content="Start Visit" Grid.Column="4"
     Command="{Binding DataContext.StartVisitCommand, ElementName=scheduleview}" CommandTarget="{Binding ElementName=scheduleview}"
    CommandParameter="{Binding SelectedAppointments, ElementName=scheduleview}"

    private void StartVisitCommandExecuted(object parameter)
        IEnumerable appointments = parameter as IEnumerable;
        SqlAppointment appt = appointments.OfType<SqlAppointment>().Where(x => x.TimeMarkerID == 3).FirstOrDefault();
    private static bool CanExecuteStartVisitCommand(object parameter)
        IEnumerable appointments = parameter as IEnumerable;
        if (appointments != null)
            SqlAppointment appt = appointments.OfType<SqlAppointment>().Where(x => x.TimeMarkerID == 3).FirstOrDefault();
            if (appt != null)
                return appt.CurrentTimeMarkerId == "3";
        return false;

  2. Answer
    Kalin avatar
    1283 posts

    Posted 13 Oct 2015 Link to this post

    Hi Brandon,

    You would need to invalidate the command in appropriate moment, in order to retrigger the CanExecute method. This can be done for example in the AppointmentSelectionChanged event handler as shown below:

    private void scheduleView_AppointmentSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        (this.DataContext as ViewModel).StartVisitCommand.InvalidateCanExecute();

    Hope this helps.

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top