2010-10-06 64 views
1

在從WinForm應用程序調用WPF KeyBindings時,我需要一些幫助。我創造了我認爲是展示問題的基本部分。如果有幫助,我可以提供示例應用程序。Winform-> WPF MVVM鍵綁定錯誤?

的WinForm應用程序啓動它有一個按鈕,調用WPF

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim view As New WpfPart.MainWindow 
    System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(view) 
    view.ShowDialog() 
End Sub 

使用WPF視圖創建它的視圖模型形式,並設置了keybings:

<Window x:Class="WpfPart.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:WpfPart.ViewModels" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.DataContext> 
    <vm:MainWindowViewModel /> 
</Window.DataContext> 
<Window.InputBindings> 
    <KeyBinding Key="Escape" Command="{Binding OpenCommand}" Modifiers="Control" /> 
</Window.InputBindings> 
<Grid> 

</Grid> 

的ViewModel使用DelagateCommand希望將所有內容鏈接起來

using System; 
using System.Windows; 
using System.Windows.Input; 
using WpfPart.Commands; 

namespace WpfPart.ViewModels 
{ 
class MainWindowViewModel 
{ 
    private readonly ICommand openCommand; 

    public MainWindowViewModel() 
    { 
     openCommand = new DelegateCommand(Open, CanOpenCommand); 
    } 

    public ICommand OpenCommand { get { return openCommand; } } 
    private bool CanOpenCommand(object state) 
    { 
     return true; 
    } 

    private void Open(object state) 
    { 
     MessageBox.Show("OpenCommand executed."); 
    } 
} 
} 

任何人都可以看到它出錯的地方,按鍵不起作用嗎?!?

+0

EnableModelessKeyboardInterop似乎爲無模式窗口縮進,但您打開一個模型窗口(ShowDialog)。你沒有嘗試過嗎? – 2010-10-06 23:10:10

+0

不,我沒有嘗試過,但是從其他概念應用程序的證明,它沒有任何版本的問題。查看解決方案的接受答案。 – Cheval 2010-10-07 01:35:05

回答

1

要使KeyBinding的工作,你需要添加一個CommandReference到你的Window.Resources,然後從你的KeyBinding(而不是命令)引用CommandReference。

我還使用了Control-X來避免在Windows中打開映射到Control-Escape的Start按鈕。

這裏是你可以使用基於你的問題的XAML:

<Window.Resources> 
    <!-- Allows a KeyBinding to be associated with a command defined in the View Model --> 
    <c:CommandReference x:Key="OpenCommandReference" Command="{Binding OpenCommand}" /> 
</Window.Resources> 
<Window.InputBindings> 
    <KeyBinding Key="X" Command="{StaticResource OpenCommandReference}" Modifiers="Control" /> 
</Window.InputBindings> 
+1

你不需要在4.0中,KeyBinding.Command屬性是一個依賴項屬性,所以你可以綁定它。無論如何,如果這是問題的原因,它將在編譯時失敗,因爲您無法綁定非依賴項屬性 – 2010-10-06 23:05:42

+0

謝謝,您是正確的,從示例中複製的代碼無法在3.5 sp1中編譯。也許Control-Escape導致了這個問題。 – Zamboni 2010-10-06 23:48:15

+0

是的,問題是兩個部分。 (ctrl-Escape)在應用程序之前被操作系統攔截,但第二個是人爲錯誤,因爲當我改變代碼(ctrl-O)時它工作正常,當我將代碼改回(ctrl-Escape)以重現該錯誤我得到了Windows攔截(開始按鈕菜單彈出),而不是沒有發生。所以我一定在第一次嘗試時在代碼中輸入了錯誤的內容。奇怪的是,輸出窗口在運行時沒有顯示任何錯誤...?不知道,但它現在工作正常,所以這個問題被回答和關閉。感謝大家的幫助。 – Cheval 2010-10-07 01:31:34

0

在M項目中,我使用的解決方案: 想,ListView控件具有與命令CmdDelete DummyViewModel的項目,我需要在調用此命令所選項目preesing刪除鍵

<Grid> 
    <Button x:Name="DeleteCmdReference" Visibility="Collapsed" Command="{Binding Source={x:Reference MyListView},Path=SelectedItem.CmdDelete}" /> 
    <ListView x:Name="MyListView" ...="" > 
     <ListView.InputBindings> 
     <KeyBinding Key="Delete" Command="{Binding ElementName=DeleteCmdReference,Path=Command}"/> 
     </ListView.InputBindings> 
    </ListView> 
    </Grid>