【发布时间】:2011-05-08 23:31:40
【问题描述】:
我有一个包含几列的 WPF DataGrid。现在我尝试将快捷键“Shift+$”添加到 DataGrid 列之一中。我想要实现的是,当用户在特定列上时,然后按“Shift + $”,它会触发命令。如果用户在其他列上,“Shift + $”作为正常输入工作。
谁能告诉我如何实现这一目标?
谢谢
精
【问题讨论】:
标签: wpf wpfdatagrid
我有一个包含几列的 WPF DataGrid。现在我尝试将快捷键“Shift+$”添加到 DataGrid 列之一中。我想要实现的是,当用户在特定列上时,然后按“Shift + $”,它会触发命令。如果用户在其他列上,“Shift + $”作为正常输入工作。
谁能告诉我如何实现这一目标?
谢谢
精
【问题讨论】:
标签: wpf wpfdatagrid
数据网格列的选项有点受限,我尝试这样做:
<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)));
}
不幸的是,这会引发一个异常,告诉您 KeyGesture 不支持 Shift+D4。我觉得你的计划可能无论如何都行不通……
【讨论】:
kb => kb.Key == e.Key && ......... 更改为kb => kb.Key == Key.Enter。应用上述更改后,当我按下箭头键等任何键时,命令也会触发。我可以将其限制为仅 Enter 键吗??
if-guards 会是什么原因。您最好提出一个新问题并提供所有必要的背景信息,以便人们重现您的问题并找到解决方案。