2010-10-06 63 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>