Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

A quick and dirty way is to hook into the FlowDocumentScrollViewer's PreviewKeyDown event and set it to handled if Ctrl + P is pressed. Here is what the code would look like:

    void fds_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key == Key.P && Keyboard.Modifiers == ModifierKeys.Control)
            e.Handled = true;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top