This is a migrated thread and some comments may be shown as answers.

Invoke Custom Command From Grid Button

1 Answer 385 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Geoff
Top achievements
Rank 1
Geoff asked on 24 Jul 2013, 05:15 PM
Hello,

I have a grid with a custom edit command and a template column with a button. I'm attempting to invoke the custom edit command from clicking the button, but the parameter object passed to the Execute method is null. This object is instantiated when using the built-in edit functionality through double-clicking on the row or pressing F2 on the keyboard, but not when the edit button is clicked.

How can I pass the EditContext parameter to my custom command from this button? Please note that I am working with the MVVM Light toolkit.

<RadGrid:RadDataGrid    Name="rdgPlay"
    Grid.Row="0"
    ItemsSource="{Binding Plays}"
    DataBindingComplete="rdgPlay_DataBindingComplete"
    UserSortMode="None"
    UserFilterMode="Disabled"
    UserGroupMode="Disabled"
    UserEditMode="Inline"
    SelectionMode="None">
    <RadGrid:RadDataGrid.Columns>
         <RadGrid:DataGridTemplateColumn CanUserEdit="False">
            <RadGrid:DataGridTemplateColumn.CellContentTemplate>
                <DataTemplate>
                    <StackPanel>
                         <Button Content="Edit">
                            <Button.Command>
                                <CustomCommands:CustomBeginEditCommand />
                            </Button.Command>
                        </Button>
                    </StackPanel>
                </DataTemplate>
            </RadGrid:DataGridTemplateColumn.CellContentTemplate>
        </RadGrid:DataGridTemplateColumn>
    </RadGrid:RadDataGrid.Columns>
    <RadGrid:RadDataGrid.Commands>
        <CustomCommands:CustomBeginEditCommand />
    </RadGrid:RadDataGrid.Commands>
</RadGrid:RadDataGrid>

public class CustomBeginEditCommand : DataGridCommand
{
  public CustomBeginEditCommand()
  {
     // Set Id so that this command can be properly associated with the desired action/event
     Id = CommandId.BeginEdit;
  }
 
  public override bool CanExecute(object parameter)
  {
    return true;
  }
 
  public override void Execute(object parameter)
  {
    base.Execute(parameter);
 
    var context = parameter as EditContext; // parameter is sometimes null
 
    // Executes the default implementation of this command
    this.Owner.CommandService.ExecuteDefaultCommand(Id, context);
  }
}

Thanks!
Geoff

1 Answer, 1 is accepted

Sort by
0
Ivaylo Gergov
Telerik team
answered on 29 Jul 2013, 01:01 PM
Hello Geoff,

Thank you for contacting us and for your question.

In this case, you execute the BeginEditCommand programmatically and you should pass the parameter. Thus, the RadDataGrid will know which row to edit. As the Button.CommandParameter does not support binding, I can suggest that you implement an attached behavior that will pass the desired EditContext  as CommandParameter. To build the EditContext you will need only the DataGridCellInfo object (the business item and the column).
You can also check the following forum post where I have showed an example of how to implement an attached behavior - http://www.telerik.com/community/forums/windows8-xaml/grid-xaml/synchronizing-selecteditems-between-a-raddatagrid-and-a-viewmodel.aspx

I hope this helps. Let me know if I can assist you in some other way.

Regards,
Ivaylo Gergov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
Grid for XAML
Asked by
Geoff
Top achievements
Rank 1
Answers by
Ivaylo Gergov
Telerik team
Share this question
or