在从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