我有一个包含Dev Express网格控件的用户控件,我试图连接一个命令以处理用户在选中某行时按下删除键的情况。无法为键绑定提供绑定
我的第一次迭代是:
<dxg:GridControl.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DataContext.DeleteItemCommand}"/>
</dxg:GridControl.InputBindings>
当在运行时用史努比检查结果我看到这个错误的结合: System.Windows.Data Error:2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.DeleteItemCommand; DataItem=null; target element is 'KeyBinding' (HashCode=39502614); target property is'Command' (type 'ICommand')
做一些研究,我发现了几个职位,说他们之后能够通过添加一个相对来源实现这一点,所以我改变了我对此的绑定:
<dxg:GridControl.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DataContext.DeleteItemCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=views:MyUserControl}}"/>
</dxg:GridControl.InputBindings>
现在在运行时,我看到这个错误: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MyProject.Views.MyUserControl', AncestorLevel='1''. BindingExpression:Path=DataContext.DeleteItemCommand; DataItem=null; target element is 'KeyBinding' (HashCode=35267081); target property is 'Command' (type 'ICommand')
在研究中我注意到这个功能是在.NET 4.0中添加的,但我使用的是.NET 4.5,所以不应该是这个问题。
我还注意到,所有的例子都在窗口级指定了InputBindings
,所以在我的应用程序中执行相同的操作是不可行的我尝试将绑定移动到用户控制级别但收到相同的结果。
也不知道它是否重要,但我的用户控件也继承了一个自定义基类,因此我不能尝试使用一个命名元素。
这不起作用,在打开的形式{X:参考本}绑定引发错误:'未解决的引用“This'' – Phaeze
我可以通过将代理绑定到'{Binding RelativeSource = {RelativeSource AncestorType = {x:Type views:MyUserControl}}来获得代码的工作方式' – Phaeze
@Peeze这是主窗口或控件的名称,你对此感兴趣。请参阅更新。 – Ilan