在從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.");
}
}
}
任何人都可以看到它出錯的地方,按鍵不起作用嗎?!?
EnableModelessKeyboardInterop似乎爲無模式窗口縮進,但您打開一個模型窗口(ShowDialog)。你沒有嘗試過嗎? – 2010-10-06 23:10:10
不,我沒有嘗試過,但是從其他概念應用程序的證明,它沒有任何版本的問題。查看解決方案的接受答案。 – Cheval 2010-10-07 01:35:05