Telerik UI for UWP RadCalendar 如何使用命令

Telerik UI for UWP RadCalendar How to use commands

我不确定如何使用 RadCalendar 命令更新我的视图模型中的 属性。我从 Telreic 获取了这个示例。

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

我创建了一个 class,如下所述:

  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;

    }
}

这是 Xaml:

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

CanExecute 和 Execute 方法在我的自定义 class 中调用正常。 如何调用 ViewModel 中的方法?

这就是我为 Teleric UWP DataGrid 实现 CellTap 命令的方式。我不知道这是否是正确的方法,如果不是,请告知:) 首先创建一个 class 并将其命名为 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);


    }
}

为您的 ViewModel 创建接口以实现:

interface IGridCommand
{
    Task CellTap(DataGridCellInfo cellInfo);
}

创建您的 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
    }

在你身上xaml:

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