Question

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?

Was it helpful?

Solution 3

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.

OTHER TIPS

You can use InputBindings and MouseBinding Descipted here :http://thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx

How about putting an InputBinding on the text block that would call the command ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top