BeginEdit optional parameter

1 Answer 136 Views
DataGrid
Greg
Top achievements
Rank 1
Greg asked on 14 Jun 2021, 03:50 PM

Hello, the documentation for the BeginEdit command shows there's a property exposed called Parameter, and it states "Gets an optional parameter holding additional information associated with the operation." However, I can't seem to be able to find any documentation regarding its usage. I would like to pass a custom object to this command, how can I accomplish this?

I'm using it inside of a Telerik RadDataGrid. The following is what the command looks like attached to the grid, where "tg" is "using:Telerik.UI.Xaml.Controls.Grid" and "local" is simply where the three command classes live in my project.

SomeArbitraryView.xaml:

<tg:RadDataGrid.Commands>
    <local:CustomBeginEditCommand  />
    <local:CustomCommitEditCommand />
    <local:CustomDataBindingCompleteCommand />
</tg:RadDataGrid.Commands>

The next snippet is what the BeginEdit class looks like, boiled down to the essentials.

CustomBeginEditCommand.cs:

using Microsoft.Toolkit.Uwp.Helpers;
using System.Collections.Generic;
using Telerik.UI.Xaml.Controls.Grid;
using Telerik.UI.Xaml.Controls.Grid.Commands;

namespace YourNamespace
{
    public class CustomBeginEditCommand : DataGridCommand
    {
        public CustomBeginEditCommand()
        {
            Id = CommandId.BeginEdit;
        }

        public override bool CanExecute(object parameter)
        {
            return true;
        }

        public override void Execute(object parameter)
        {
            var context = parameter as EditContext;
            DataGridCellInfo cellInfo = context.CellInfo;

            // Here is what Telerik offers us to access this optional parameter, but it's always null by default until I can figure out how to pass my custom object
            var optionalParam = context.Parameter;

            // ...

            Owner.CommandService.ExecuteDefaultCommand(CommandId.BeginEdit, context);
        }
    }
}

My knee-jerk reaction is to have something right in the XAML that I can fill with my custom object, but that doesn't seem to be a valid solution. Any help is appreciated!

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 14 Jun 2021, 05:41 PM

Hi Greg,

The parameter usually comes from the command binding. However, in this case you are adding a using a custom command instance to the Commands collection, so you're not actually binding anything.

My recommendation would be to just add a property to your custom command and use it directly

Adding a Property

This is like any other class that you want to add a property to, so you could do soemthign lie this:

public class CustomBeginEditCommand : DataGridCommand
{
    public string MyProperty {get;set;}
}

However, I recommend that you use a DependencyProperty instead. That you can bind to it.

public class CustomBeginEditCommand : DataGridCommand
{
    public CustomBeginEditCommand()
    {
        Id = CommandId.CellTap;
    }

    public override bool CanExecute(object parameter)
    {
        return true;
    }

    public override void Execute(object parameter)
    {
        if(parameter is EditContext context)
        {
            DataGridCellInfo cellInfo = context.CellInfo;

            if (!string.IsNullOrEmpty(this.MyProperty))
            {
                // Do something with your custom property
                var x = this.MyProperty;

            }

            Owner.CommandService.ExecuteDefaultCommand(CommandId.CellTap, context);
        }
    }

    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
        "MyProperty", typeof(string), typeof(CustomBeginEditCommand), new PropertyMetadata(default(string)));

    public string MyProperty
    {
        get => (string) GetValue(MyPropertyProperty);
        set => SetValue(MyPropertyProperty, value);
    }
}

 

To use it, simply bind/pass the parameter value you want:

<tg:RadDataGrid.Commands>
    <local:CustomBeginEditCommand MyProperty="{Binding ParameterValueToPassToCommand}" />
</tg:RadDataGrid.Commands>

 

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
DataGrid
Asked by
Greg
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or