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

RadGridView DESELECT a Row

3 Answers 407 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Devid
Top achievements
Rank 1
Devid asked on 19 Jan 2017, 10:29 AM
After I open my application and select a Row in RadGridView, I would like on Esc button click to deselect that row, is that possible ? If yes how ?
Thanks

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 20 Jan 2017, 05:02 PM
Hello Devid,

The desired behavior can be achieved by creating a custom KeyboardCommandProvider and overriding it's ProvideCommandsForKey like so:

public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
{
    List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
 
    if (key == Key.Escape)
    {
        commandsToExecute.Clear();
        parentGrid.SelectedItems.Clear();
    }
 
    return commandsToExecute;
}

Please let me know if this works for you.

Regards,
Dilyan Traykov
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Devid
Top achievements
Rank 1
answered on 24 Jan 2017, 09:38 AM

Thank you. I added a event KeyDown  to my RadGridView and implemented it like this and it works:

 

private void GlobalUiRadGridView_OnKeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.Escape))
            {
                this.MyRadGridView.KeyboardCommandProvider = new                                     EscapeCustomKeyboardCommandProvider(this.MyRadGridView);
            }
        }
0
Accepted
Dilyan Traykov
Telerik team
answered on 24 Jan 2017, 11:45 AM
Hello Devid,

Rather than set RadGridView's KeyboardCommandProvider each time the Escape key is pressed, I would suggest you set that only once in the window's constructor as suggested in the article I referenced. You may also do that in RadGridView's Loaded event.

public MainWindow()
{
    InitializeComponent();
    this.MyRadGridView.KeyboardCommandProvider = new EscapeCustomKeyboardCommandProvider(this.MyRadGridView);
}

public class EscapeCustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
{
    private GridViewDataControl parentGrid;
 
    public EscapeCustomKeyboardCommandProvider(GridViewDataControl grid)
     : base(grid)
    {
        this.parentGrid = grid;
    }
 
    public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
    {
        List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
 
        if (key == Key.Escape)
        {
            commandsToExecute.Clear();
            parentGrid.SelectedItems.Clear();
        }
 
        return commandsToExecute;
    }
}

I hope you find this suggestion helpful.

Regards,
Dilyan Traykov
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Devid
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Devid
Top achievements
Rank 1
Share this question
or