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

Binding KeyboardCommandProvider in MVVM scenario

1 Answer 241 Views
GridView
This is a migrated thread and some comments may be shown as answers.
SteSa
Top achievements
Rank 1
SteSa asked on 27 Nov 2018, 09:34 AM

     Hi,

i'm trying to use KeyboardCommandProvider, with binding in a MVVM scenario for managing the Enter key(i don't want to move down). I've read this post, https://docs.telerik.com/devtools/wpf/controls/radgridview/commands/keyboardcommandprovider and i try to implement my CustomKeyboardCommandProvider without success; i've a usercontrol with insede a GridView but i've an error message: "The type CustomKeyboardCommandProvider does not include any accessibile constructor". How can i solve it in my scenario?

This is my code:

XAML:

<UserControl.Resources>
        <Conditional:CustomKeyboardCommandProvider x:Key="MyKeyboardCommand" />
    </UserControl.Resources>

<telerik:RadGridView Grid.Column="0" Grid.Row="1" Name="MyRadGridArtworks"
                                ItemsSource="{Binding GridViewList, Mode=TwoWay}"
                                SelectedItem="{Binding GridViewRowSelected, Mode=TwoWay}"                                            
                                EnableColumnVirtualization="False"
                                ValidatesOnDataErrors="None"              
                                ShowColumnSortIndexes="False"
    AutoGenerateColumns="False"
                                telerikControls:StyleManager.Theme="Office2013"                                          
    ColumnWidth="auto"                                         
    CanUserFreezeColumns="True"
                                LeftFrozenColumnCount="3"
                                ShowColumnFooters="True"                                      
                                KeyboardCommandProvider="{StaticResource MyKeyboardCommand}"
                                IsLocalizationLanguageRespected="False"
    RowIndicatorVisibility="Collapsed">

</telerik:RadGridView>

C# --> CustomKeyboardCommandProvider

public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
    {
        private GridViewDataControl dataControl;

        public CustomKeyboardCommandProvider(GridViewDataControl dataControl ) : base(dataControl)
        {
            this.dataControl = dataControl;
        }

        public override IEnumerable<System.Windows.Input.ICommand> ProvideCommandsForKey(System.Windows.Input.Key key)
        {

            if (key != System.Windows.Input.Key.Return)
                // use default behavior for all keys except Return
                return base.ProvideCommandsForKey(key);

            List<ICommand> commandsToExecute = new List<ICommand>();

            if (this.dataControl.CurrentCell == null)
                return commandsToExecute;

            if (this.dataControl.CurrentCell.IsInEditMode)
            {
                commandsToExecute.Add(RadGridViewCommands.CommitEdit);
            }
            else
            {
                commandsToExecute.Add(RadGridViewCommands.ActivateRow);
            }

            return commandsToExecute;
        }
    }

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 29 Nov 2018, 11:13 AM
Hello Stefano,

As the error suggests, the CustomKeyboardCommandProvider class does not provide a default constructor and instead needs to be passed in a RadGridView instance with which to operate.

Thus, you can either initialize it in code-behind as demonstrated in this article or use one of the approaches suggested in this forum thread should you find them applicable.

Do let me know if any of these approaches works for you.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
SteSa
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or