Search code examples
uwptelerik

Telerik UI for UWP RadCalendar How to use commands


I am not sure how to use The RadCalendar comands to update a property in my view model. I follwed this sample from Telreic.

https://docs.telerik.com/devtools/universal-windows-platform/controls/radcalendar/commands/celltap

I created a class as stated:

  public class CustomCellTapCommand : CalendarCommand
{
    public CustomCellTapCommand()
    {
        Id = CommandId.CellTap;
    }


    public override bool CanExecute(object parameter)
    {
        CalendarCellTapContext ctx = parameter as CalendarCellTapContext;
        var date = ctx.CellModel.Date;
        if (date > DateTime.Now)
        {
            return false;
        }

        return true;
    }

    public override void Execute(object parameter)
    {
        CalendarCellModel context = parameter as CalendarCellModel;

    }
}

Heres is the Xaml:

<Custom:RadCalendar.Commands>
    <local:CustomCellTapCommand/>
</Custom:RadCalendar.Commands>

The CanExecute and Execute methods are being called ok in my custom class. How do I call the methods in the ViewModel?


Solution

  • This is how I implemented a CellTap Command for the Teleric UWP DataGrid. I don't know if it is the correct way please advise if it is not :) First create a class and call it CellTapCmd:

    public class CellTapCmd: DataGridCommand
    {
        private Object _viewModel;
        public Object viewModel
        {
            get { return _viewModel; }
            set
            {
                _viewModel = value;
            }
        }
        public CellTapCmd()
        {
            this.Id = CommandId.CellTap;
        }
    
        public override bool CanExecute(object parameter)
        {
            var context = parameter as DataGridCellInfo;
            // put your custom logic here
            return true;
        }
    
        public async override void Execute(object parameter)
        {
            var context = parameter as DataGridCellInfo;
            // put your custom logic here
           await ((IGridCommand)viewModel).CellTap(context);
    
    
        }
    }
    

    Create an Interface for you ViewModel to implement:

    interface IGridCommand
    {
        Task CellTap(DataGridCellInfo cellInfo);
    }
    

    Create you ViewModel:

      public class AnyViewModel : ViewModelBase,IGridCommand
    {
        private DataGridCommand _cellTapCmd;
        public DataGridCommand cellTapCmd
        {
            get { return _cellTapCmd; }
            set
            {
               SetProperty(ref _cellTapCmd,value);
                CellTapCmd cmd = (CellTapCmd)value;
                cmd.viewModel = this;
            }
        }
    
        public async Task CellTap(DataGridCellInfo cellInfo)
        {
            //do something amazing here
        }
    

    In you xaml:

        <telerik:RadDataGrid.Commands>
            <gridCommands:DataGridUserCommand Command="{x:Bind Path=ViewModel.cellTapCmd, Mode=TwoWay}" Id="CellTap" />
        </telerik:RadDataGrid.Commands>