【发布时间】:2015-03-20 06:31:39
【问题描述】:
您好,我在 WPF DataGrid 上有两个输入键绑定,当按下键时会调用相应的命令,如下所示:
<DataGrid Grid.Row="1" IsReadOnly="False" Focusable="True"
AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserDeleteRows="False"
EnableColumnVirtualization="False" EnableRowVirtualization="False" SelectionMode="Single"
ItemsSource="{Binding Path=QueryClauses}"
SelectedIndex="{Binding Path=SelectedQueryClauseIndex}">
<DataGrid.InputBindings>
<KeyBinding Key="Insert" Command="{Binding DataContext.InsertQueryClause, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"/>
<KeyBinding Key="Delete" Command="{Binding DataContext.DeleteQueryClause, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"/>
</DataGrid.InputBindings>
...
</DataGrid>
一开始它工作得很好,但是在可编辑的DataGrid上编辑/删除一段时间后,“插入/删除”命令停止工作,我在命令中调试,该命令从未被触发,这意味着DataGrid不知何故失去了对键盘输入绑定的跟踪。
我搜索并发现有人遇到了类似的问题: https://social.msdn.microsoft.com/Forums/vstudio/en-US/0cdabbe0-b96f-43a2-bcb0-842d1766c2c7/why-does-my-inputbinding-stop-working?forum=wpf
帖子解释说,控件丢失了绑定,因为它以某种方式看到了 e.handled = true,因此绕过了输入。
但是,这篇文章没有说明如何修复它,所以有人对此有任何想法吗?
谢谢!
【问题讨论】:
标签: wpf datagrid key-bindings