Search code examples
wpfdialogcommandmouseeventrouted-commands

Show dialog box on shift double click


My WPF application has a TextBlock control in it. I want to display an "easter egg" when the user holds down the shift key and right double clicks on it.

I've added a RoutedUiCommand to a static class in my application where I've defined all of the commands. I've added a command binding for my new command:

<UserControl.CommandBindings>
    <CommandBinding CanExecute="ShowDiagnostics_CanExecute" Command="cs:CarSystemCommands.ShowDiagnostics"  Executed="ShowDiagnostics_Executed" />
</UserControl.CommandBindings>

When I created the RoutedUiCommand, I specified a MouseGesture of RightDoubleClick with a ModifierKey of Shift. So far so good.

How do I associate the command with the TextBlock?


Solution

  • What I ended up doing was to move the CommandBinding from the UserControl.Resources to the TextBlock:

    <TextBlock ...>
        <TextBlock.CommandBindings>
            <CommandBinding CanExecute="ShowDiagnostics_CanExecute" Command="cs:CarSystemCommands.ShowDiagnostics"  Executed="ShowDiagnostics_Executed" />
        </TextBlock.CommandBindings>
    </TextBlock>
    

    Now, nothing happens until you hold down the shift key & right double click on the TextBlock.

    I gave the other answers up votes because they'll work, too.