【问题标题】:MVVM C# WPF binding mouse double clickMVVM C#​​ WPF 绑定鼠标双击
【发布时间】:2011-04-16 14:08:15
【问题描述】:

我想通过单击鼠标将一个文本框的内容复制到另一个文本框中。

如何绑定鼠标点击事件?

【问题讨论】:

  • 能否再描述一下鼠标点击的场景。鼠标是否必须位于特定区域或某个控件上,或者只需单击鼠标?您还想模拟单击还是双击?
  • 也许这回答了你的问题:stackoverflow.com/questions/1035023/…

标签: wpf mvvm


【解决方案1】:

此示例适用于RightClick,但您可以根据需要调整事件:

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

编辑:我在我的 SkyDrive 上上传了一个 sample app,它说明了如何使用此方法来实现您所需要的。请注意,它仅适用于 .NET Framework 4+

【讨论】:

  • 这行得通吗?我不认为你可以像这样使用绑定......它会抱怨“不能在'MouseBinding'类型的'Command'属性上设置'Binding'。'Binding'只能在 DependencyProperty 上设置一个依赖对象。你怎么看?
  • 不幸的是,由于这个原因它不会起作用:connectppe.microsoft.com/VisualStudio/feedback/details/687703/…
  • 好的,伙计们,它只在面向 .NET 4 时才有效。对于以前的版本,它会抛出 Ann Sanderson 指定的错误
【解决方案2】:

想要向控件添加行为?只需使用Ramora pattern

【讨论】:

  • 虽然我承认 Ramora 模式的实用性,但我认为它的复杂性不适合 user456871 的需要;还有其他更简单的解决方案
【解决方案3】:

希望对您有所帮助

将此代码用于TreeView

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

将此代码用于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>

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

【讨论】:

  • 很好的答案,谢谢!注意:在 ItemContainerStyle 中,您可能必须使用 Command 绑定 {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.YourCommand},假设顶层控件是 Window(切换到 UserControl 或其他任何合适的控件)
【解决方案4】:

听起来你正在为你的文本框发明一种新的行为:)

我会考虑您的程序的用户是否理解并喜欢这种行为。

如果它只是一个你必须点击的按钮,也许更容易理解它的功能 - 实现起来也更快:)

【讨论】:

  • +1。我讨厌那些试图在交互方案上过于创新的应用
【解决方案5】:

我认为您可以将鼠标手势绑定到命令。看看这个:http://www.thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx

【讨论】:

    【解决方案6】:

    我不确定你到底想绑定什么。

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

    您在Button 上找到的Click 事件是从ButtonBase 继承的,并且在大多数控件上并不容易使用。

    MouseDoubleClick 继承自 Control 并且可用于从它派生的任何东西。

    在您的示例中,它听起来像一个简单的 Button 处理其 Click 事件可能会成功。

    要绑定到点击事件,只需要在Button中指定事件的事件处理程序即可。

    类似:

    XAML:

    <TextBox Name=TextBoxOne />
    <TextBox Name=TextBoxTwo />
    
    <Button Click="CopyTextButton_Click"/>
    

    在你的代码后面:

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

    否则,如果这是一个更专业的场景,您可能需要使用 UserControl 或 AndrewS 上面回答的命令进行调查。

    希望对你有帮助。

    【讨论】:

    • 好的,谢谢。我想在文本框“后面”放置一个按钮(自定义文本框+按钮用户控件)来实现这一点。因此,当用户单击文本时,按钮会收到事件...谢谢大家的帮助。我会试试这个。
    【解决方案7】:

    您可以通过创建新行为轻松做到这一点。

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

    下面是代码:

    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();
            }
        }
    }
    

    请根据您的需要更新此 sn-p。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 2019-02-09
      • 2010-12-10
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      相关资源
      最近更新 更多