【问题标题】:Disable a FlowDocumentScrollViewer's print command binding (and use my Window's instead!)禁用 FlowDocumentScrollViewer 打印命令绑定(并改用我的 Windows!)
【发布时间】:2011-05-13 13:21:07
【问题描述】:

在我的 WPF 应用程序中,我创建了一个 Window,其中包含一个 FlowDocumentScrollViewer 以及其他控件。

我已经为我的 Window 创建了一个命令绑定到 Print 命令,其中包含一个 Executed 处理程序,该处理程序运行一些自定义逻辑,并最终打印 FlowDocumentScrollViewer 的内容。

一切正常,但我有一个问题。

如果用户在 FlowDocumentScrollViewer 中单击,然后按 Ctrl + P,它将执行 FlowDocumentScrollViewer 本身的打印命令绑定,而不是我的窗口的绑定。所以我的自定义逻辑没有执行,打印输出也不是应该的。

如何禁用 FlowDocumentScrollViewer 的打印命令绑定,并确保在所有情况下按 Ctrl + P 都会运行我的 Windows 的打印命令绑定?

【问题讨论】:

    标签: .net wpf flowdocumentscrollviewer


    【解决方案1】:

    一种快速而肮脏的方法是挂钩 FlowDocumentScrollViewer 的 PreviewKeyDown 事件,并在按下 Ctrl + P 时将其设置为已处理。下面是代码的样子:

        void fds_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.P && Keyboard.Modifiers == ModifierKeys.Control)
                e.Handled = true;
        }
    

    【讨论】:

    • 很酷,确实可行 - 我过去曾在其他情况下使用过这种技术!但我让它以一种稍微干净的方式工作(见我的回答)。谢谢。
    【解决方案2】:

    我通过从窗口中删除事件处理程序并将其直接连接到 FlowDocumentScrollViwer 来使其工作:

    <FlowDocumentScrollViewer x:Name="MyFlowDocumentScrollViewer">
        <FlowDocumentScrollViewer.CommandBindings>
            <CommandBinding Command="Print" Executed="Print_Executed" />
        </FlowDocumentScrollViewer.CommandBindings>
    </FlowDocumentScrollViewer>
    

    然后我必须将任何其他打印命令控件(例如我的工具栏按钮)的 CommandTarget 直接绑定到 FlowDocumentScrollViewer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2013-10-08
      相关资源
      最近更新 更多