【发布时间】:2011-04-16 14:08:15
【问题描述】:
我想通过单击鼠标将一个文本框的内容复制到另一个文本框中。
如何绑定鼠标点击事件?
【问题讨论】:
-
能否再描述一下鼠标点击的场景。鼠标是否必须位于特定区域或某个控件上,或者只需单击鼠标?您还想模拟单击还是双击?
-
也许这回答了你的问题:stackoverflow.com/questions/1035023/…
我想通过单击鼠标将一个文本框的内容复制到另一个文本框中。
如何绑定鼠标点击事件?
【问题讨论】:
此示例适用于RightClick,但您可以根据需要调整事件:
<TextBox>
<TextBox.InputBindings>
<MouseBinding Gesture="RightClick" Command="{Binding YourCommand}" />
</TextBox.InputBindings>
</TextBox>
编辑:我在我的 SkyDrive 上上传了一个 sample app,它说明了如何使用此方法来实现您所需要的。请注意,它仅适用于 .NET Framework 4+
【讨论】:
想要向控件添加行为?只需使用Ramora pattern !
【讨论】:
将此代码用于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);
}
}
【讨论】:
{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.YourCommand},假设顶层控件是 Window(切换到 UserControl 或其他任何合适的控件)
听起来你正在为你的文本框发明一种新的行为:)
我会考虑您的程序的用户是否理解并喜欢这种行为。
如果它只是一个你必须点击的按钮,也许更容易理解它的功能 - 实现起来也更快:)
【讨论】:
【讨论】:
我不确定你到底想绑定什么。
据我所知,没有现成的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 上面回答的命令进行调查。
希望对你有帮助。
【讨论】:
您可以通过创建新行为轻松做到这一点。
<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。
【讨论】: