.netwpfflowdocumentscrollviewer

Disable a FlowDocumentScrollViewer's print command binding (and use my Window's instead!)


In my WPF app, I have created a Window which contains a FlowDocumentScrollViewer, amongst other controls.

I have created a command binding for my Window to the Print command, with an Executed handler that runs some custom logic, and ends up printing the content of the FlowDocumentScrollViewer.

All works well, but I am having one problem.

If the user clicks within the FlowDocumentScrollViewer, and then presses Ctrl + P, it executes the Print command binding of the FlowDocumentScrollViewer itself, not that of my Window. And so my custom logic is not executed, and the printout is not as it should be.

How can I disable the FlowDocumentScrollViewer's Print command binding, and ensure that pressing Ctrl + P runs my Windows's Print command binding in all cases?


Solution

  • I got it working by removing the event handler from my window, and hooking it up directly to the FlowDocumentScrollViwer instead:

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

    I then had to bind the CommandTarget of any other print command controls (e.g. my toolbar button) directly to the FlowDocumentScrollViewer.