MVVM C# WPF binding mouse double click

2019-08-01 21:56发布

I want to copy the content of one text box to another text box by clicking the mouse.

How do I bind a mouse click event?

标签: wpf mvvm
7条回答
何必那么认真
2楼-- · 2019-08-01 22:02

I think you could bind mouse gestures to commands. Take a look at this: http://www.thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-01 22:07

You can easily do this by creating a new behavior.

<TextBox 
    MouseDoubleClick="SelectAddress" 
    GotKeyboardFocus="SelectAddress" 
    PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton" />

Here's the code behind:

private void SelectAddress(object sender, RoutedEventArgs e)
{

    TextBox tb = (sender as TextBox);
    if (tb != null)
    {
        tb.SelectAll();
    }

}

private void SelectivelyIgnoreMouseButton(object sender, 
        MouseButtonEventArgs e)

{

    TextBox tb = (sender as TextBox);
    if (tb != null)
    {
         if (!tb.IsKeyboardFocusWithin)
        {
            e.Handled = true;
            tb.Focus();
        }
    }
}

Please update this snippet according to your need.

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-01 22:09

It sounds like you are inventing a new behaviour for your textbox :)

I would just consider if the users of your program understands and likes this behaviour.

Maybe it is easier to understand the funcionality if it is just a button you have to click - it is also faster to implement :)

查看更多
beautiful°
5楼-- · 2019-08-01 22:12

Hope this helps

Use this code for TreeView

<TreeView commandBehaviors:MouseDoubleClick.Command="{Binding YourCommand}"
          commandBehaviors:MouseDoubleClick.CommandParameter="{Binding}"
          .../>

Use this code for TreeViewItem

<TreeView ItemsSource="{Binding Projects}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="commandBehaviors:MouseDoubleClick.Command"
                    Value="{Binding YourCommand}"/>
            <Setter Property="commandBehaviors:MouseDoubleClick.CommandParameter"
                    Value="{Binding}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

Use this code to create a new behavior MouseDoubleClick

public class MouseDoubleClick
{
    public static DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command",
        typeof(ICommand),
        typeof(MouseDoubleClick),
        new UIPropertyMetadata(CommandChanged));

    public static DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter",
                                            typeof(object),
                                            typeof(MouseDoubleClick),
                                            new UIPropertyMetadata(null));

    public static void SetCommand(DependencyObject target, ICommand value)
    {
        target.SetValue(CommandProperty, value);
    }

    public static void SetCommandParameter(DependencyObject target, object value)
    {
        target.SetValue(CommandParameterProperty, value);
    }
    public static object GetCommandParameter(DependencyObject target)
    {
        return target.GetValue(CommandParameterProperty);
    }

    private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        Control control = target as Control;
        if (control != null)
        {
            if ((e.NewValue != null) && (e.OldValue == null))
            {
                control.MouseDoubleClick += OnMouseDoubleClick;
            }
            else if ((e.NewValue == null) && (e.OldValue != null))
            {
                control.MouseDoubleClick -= OnMouseDoubleClick;
            }
        }
    }

    private static void OnMouseDoubleClick(object sender, RoutedEventArgs e)
    {
        Control control = sender as Control;
        ICommand command = (ICommand)control.GetValue(CommandProperty);
        object commandParameter = control.GetValue(CommandParameterProperty);
        command.Execute(commandParameter);
    }
}
查看更多
手持菜刀,她持情操
6楼-- · 2019-08-01 22:18

This sample is for RightClick, but you can adjust the event according to your needs:

<TextBox>
    <TextBox.InputBindings>
        <MouseBinding Gesture="RightClick" Command="{Binding YourCommand}" />
    </TextBox.InputBindings>
</TextBox>

Edit: I uploaded on my SkyDrive a sample app that illustrates how to use this method in order to achieve exactly what you need. Please be advised that it will only work for .NET Framework 4+

查看更多
混吃等死
7楼-- · 2019-08-01 22:18

Want to add a behavior to a control ? Just use the Ramora pattern !

查看更多
登录 后发表回答