MVVM c# WPF绑定鼠标双击

时间:2022-12-09 21:13:47

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?

如何绑定鼠标单击事件?

7 个解决方案

#1


4  

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+

编辑:我在我的SkyDrive上上传了一个示例应用,演示了如何使用这个方法,以实现您所需要的。请注意,它只适用于。net Framework 4+

#2


3  

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

想要将行为添加到控件中?就用Ramora模式吧!

#3


1  

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 :)

如果你只需要点击一个按钮,或许更容易理解它的趣味性——实现起来也更快。

#4


0  

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

我认为你可以把鼠标手势和命令绑定在一起。看一下这个:http://www.thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx。

#5


0  

I'm not sure what exactly you're wanting to bind to.

我不知道你到底想要什么。

There is no readily available MouseClick event as far as i'm aware.

据我所知,目前还没有现成的MouseClick事件。

the Click event as you'd find on a Button is inherited from ButtonBase and is not readily available on most controls.

在按钮上发现的单击事件是从ButtonBase继承的,在大多数控件上都不容易获得。

MouseDoubleClick is inherited from Control and available on anythning deriving from it.

MouseDoubleClick从控件继承而来,可以从它派生的任何内容上使用。

in your example it sounds like a simple Button with its Click event handled might do the trick.

在您的示例中,它听起来像一个简单的按钮,并处理它的单击事件可能会达到这个目的。

To bind to the click event, you just need to specify the event handler for the event in the Button.

要绑定到单击事件,只需为按钮中的事件指定事件处理程序。

Something like:

喜欢的东西:

XAML:

XAML:

<TextBox Name=TextBoxOne />
<TextBox Name=TextBoxTwo />

<Button Click="CopyTextButton_Click"/>

And in your code behind:

在你的代码后面:

void CopyTextButton_Click(object sender, RoutedEventArgs e)
{
    //Copy the text and anything else you need done
}

Otherwise if this is a more specialised scenario, you might want to investigate using a UserControl or as AndrewS answered above, a Command.

否则,如果这是一个更专门化的场景,您可能希望使用UserControl或如AndrewS上面回答的那样使用命令进行调查。

Hope it helps.

希望它可以帮助。

#6


0  

Hope this helps

Use this code for TreeView

使用此代码为TreeView。

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

Use this code for TreeViewItem

使用此代码处理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

使用此代码创建一个新的行为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);
    }
}

#7


0  

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.

请根据您的需要更新这段代码。

#1


4  

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+

编辑:我在我的SkyDrive上上传了一个示例应用,演示了如何使用这个方法,以实现您所需要的。请注意,它只适用于。net Framework 4+

#2


3  

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

想要将行为添加到控件中?就用Ramora模式吧!

#3


1  

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 :)

如果你只需要点击一个按钮,或许更容易理解它的趣味性——实现起来也更快。

#4


0  

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

我认为你可以把鼠标手势和命令绑定在一起。看一下这个:http://www.thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx。

#5


0  

I'm not sure what exactly you're wanting to bind to.

我不知道你到底想要什么。

There is no readily available MouseClick event as far as i'm aware.

据我所知,目前还没有现成的MouseClick事件。

the Click event as you'd find on a Button is inherited from ButtonBase and is not readily available on most controls.

在按钮上发现的单击事件是从ButtonBase继承的,在大多数控件上都不容易获得。

MouseDoubleClick is inherited from Control and available on anythning deriving from it.

MouseDoubleClick从控件继承而来,可以从它派生的任何内容上使用。

in your example it sounds like a simple Button with its Click event handled might do the trick.

在您的示例中,它听起来像一个简单的按钮,并处理它的单击事件可能会达到这个目的。

To bind to the click event, you just need to specify the event handler for the event in the Button.

要绑定到单击事件,只需为按钮中的事件指定事件处理程序。

Something like:

喜欢的东西:

XAML:

XAML:

<TextBox Name=TextBoxOne />
<TextBox Name=TextBoxTwo />

<Button Click="CopyTextButton_Click"/>

And in your code behind:

在你的代码后面:

void CopyTextButton_Click(object sender, RoutedEventArgs e)
{
    //Copy the text and anything else you need done
}

Otherwise if this is a more specialised scenario, you might want to investigate using a UserControl or as AndrewS answered above, a Command.

否则,如果这是一个更专门化的场景,您可能希望使用UserControl或如AndrewS上面回答的那样使用命令进行调查。

Hope it helps.

希望它可以帮助。

#6


0  

Hope this helps

Use this code for TreeView

使用此代码为TreeView。

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

Use this code for TreeViewItem

使用此代码处理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

使用此代码创建一个新的行为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);
    }
}

#7


0  

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.

请根据您的需要更新这段代码。