我有一個包含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