wpfxamlmvvminputbinding

Double click mouse action of TextBox is not allowed


I am trying to set the double click gesture of a text box in this way:

<TextBox>
    <TextBox.InputBindings>
        <MouseBinding Gesture="MouseDoubleClick" Command="{Binding DobleClickCommand}"/>
    </TextBox.InputBindings>
</Textbox>

But I get the error that the mouse action MOUSEDOUBLECLICK is not allowed.

I would like to bind it to the command in my view model.

Is input bindings the way to do it?


Solution

  • Your mouse binding is wrong, there is no MouseDoubleClick, use LeftDoubleClick. For a list of valid mouse actions, look in the documentation. If you want to execute the action for MiddleDoubleClick or RightDoubleClick, too, you can create separate mouse bindings for them to the same command.

    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding TestingCommand}"/>
    

    Alternatively, you can use MouseAction instead of a Gesture:

    <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding DobleClickCommand}"/>
    

    The documentation recommends to use Gesture instead of MouseAction:

    When defining a MouseBinding in Extensible Application Markup Language (XAML), there are two ways to specify the MouseGesture. The first way to establish a MouseBinding in XAML is to define the Gesture attribute of the MouseBinding element, which enables a syntax to specify mouse actions and modifiers as a single string; for example, "CTRL+LeftClick". The second way is to define the MouseAction attribute of the MouseBinding element.

    In general, it is recommended that you use only the Gesture attribute from XAML, even if you do not specify modifiers; this avoids ambiguity, provides the most streamlined syntax, and provides the most straightforward representation for serialization.

    On the difference between a MouseAction and a Gesture:

    A MouseGesture is a MouseAction with or without a set of ModifierKeys. Unlike a KeyGesture, a MouseGesture does not need to have a modifier key associated with it.