0

I have a WPF DataGrid with few columns. Now i try to add the short cut key "Shift+$" into one of the DataGrid column. What I want to achieve is that when user on the partipular column, then press "Shift + $", it fire command. If user on other columns, "Shift + $" works as normal input.

Can anyone give me some idea how I can achieve this?

Thanks

Jing

Jing
  • 341
  • 5
  • 22

1 Answers1

0

The options are a bit limited with datagrid columns, i tried doing it this way:

<DataGridTextColumn Binding="{Binding Name}">
    <DataGridTextColumn.EditingElementStyle>
        <Style>
            <EventSetter Event="FrameworkElement.Loaded" Handler="DG_NameColumn_Loaded"/>
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
private void DG_NameColumn_Loaded(object sender, RoutedEventArgs e)
{
    var tb = sender as TextBox;
    tb.InputBindings.Add(new KeyBinding(Commands.DoStuff, new KeyGesture(Key.D4, ModifierKeys.Shift)));
}

Unfortunately this throws an exception telling you that Shift+D4 is not supported by KeyGesture. I think your plan might not work out anyway...

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Hi, thanks for the reply. Also tried Shift+D4, it is not supported. What you said is there si no solution for my problem? – Jing May 10 '11 at 01:47
  • @H.B. I also have similar problem. I have a textbox in DataGridColumnHeader, And I wish that I could fire a command when I press enter on that TextBox. Is that possible? I have tried this answer: http://stackoverflow.com/a/12943306/2284240 , But I was not as much lucky. I mean I changed `kb => kb.Key == e.Key && .........` to `kb => kb.Key == Key.Enter`. After applying above changes, When I press any key like arrow key, then also, the command fires. Can I restrict that to only Enter key?? – Vishal Apr 06 '15 at 00:35
  • @Vishal: Sorry, i don't know why that would be if you have `if`-guards set accordingly. You better ask a new question and give all the necessary context so people can reproduce your problem and find solutions. – H.B. Apr 06 '15 at 06:54
  • @H.B. Thanks for your reply. I will post a new question. – Vishal Apr 06 '15 at 10:32