【问题标题】:WPF How to handle Cut, Copy, Paste toolbar buttons with UserControls and WindowsFormsHostWPF 如何使用 UserControls 和 WindowsFormsHost 处理剪切、复制、粘贴工具栏按钮
【发布时间】:2014-11-19 10:46:44
【问题描述】:

我希望有人可以帮助我。我正在尝试使用 MVVM 将旧版 winforms 应用程序转换为 WPF。我已将应用程序的主窗口分成 4 个主要的用户控件。 UserControls 显示不同类型的数据对象,每个 UserControl 都有自己的 ViewModel。一些数据对象可以在不同的用户控件之间互换,例如“用户控件 1”可以包含字符串对象,“用户控件 2”和“用户控件 3”也可以包含(见下图)。

我的问题是如何处理工具栏中的剪切、复制、粘贴命令?为了可能使事情变得更复杂,每个 UserControl 可以同时包含一个选定对象,因为其他 UserControls 包含一个选定对象,并且 User Control 2 是一个围绕 winforms 控件的 WindowsFormsHost 包装器。

到目前为止,我已经尝试过使用 ApplicationCommands,但我什至无法启动它们。我已经粘贴了我认为可以使用下面的 ApplicationCommands 工作的代码的 sn-p。对此的任何帮助将不胜感激。

<Button Command="ApplicationCommands.Cut" />

在用户控件上

  <UserControl.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Cut" Executed="executed_Cut" CanExecute="canExecute_Cut" />
  </UserControl.CommandBindings>

最后在 UserControl 的代码后面(我知道这不是很好)

    public void executed_Cut(Object sender, ExecutedRoutedEventArgs e)
    {
        //execute code here
    }

    public void canExecute_Cut(Object sender, CanExecuteRoutedEventArgs e)
    {
        //can execute code here  
    }

【问题讨论】:

  • 您需要的第一件事是在 canExecute_Cut 中启用类似这样的按钮。 e.CanExecute = true; //或您的应用程序中的其他一些条件。
  • 嗨,stratonn,对不起,我应该说;我根据我的代码要求在 canExecute_Cut 方法中设置 e.CanExecute 等于 true 或 false。我只是在这里省略了它,因为目前从不调用 execute_Cut 和 canExecute_Cut。

标签: wpf user-controls copy


【解决方案1】:

我成功地使用了this question 中描述的行为方法来避免在我的视图模型中放置任何代码。然后,每当焦点转到定义了复制/粘贴行为的控件时,工具栏的剪切/复制/粘贴按钮就会相应地“亮起”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多