WPF DataGrid输入键绑定一段时间后停止工作?

时间:2015-03-20 06:31:39

标签: wpf datagrid key-bindings

您好我在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,因此输入被绕过了。

但是,这篇文章没有说明如何修复它,所以任何人都对此有任何想法?

谢谢!

1 个答案:

答案 0 :(得分:0)

检查了其他帖子,似乎设置了以下属性解决了问题:

Focusable="True"

感谢。