C# WPF窗口边框上的双击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9213030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Double-click event on WPF Window border
提问by Terminador
Is there any event which fires on double-click event on WPF Window border?
是否有任何事件在 WPF 窗口边框上触发双击事件?
How I can catch it?
我怎么能抓住它?
Thanks!
谢谢!
采纳答案by RQDQ
Here is one way.. Just set your Window.WindowStyle to "None" and create your down window border:
这是一种方法..只需将您的 Window.WindowStyle 设置为“无”并创建您的向下窗口边框:
<Grid>
<Border
BorderBrush="Silver"
BorderThickness="10"
Name="border1"
MouseLeftButtonDown="border1_MouseLeftButtonDown" />
</Grid>
In code behind:
在后面的代码中:
private void border1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
MessageBox.Show("Double Click");
}
回答by frosteroid
using System.Windows.Input;
using System.Windows.Threading;
namespace System.Windows.Controls{
class DCCanvas : Canvas{
public event MouseButtonEventHandler MouseDoubleClick;
private bool doubleClickStarted;
private DispatcherTimer doubleClickTimer;
private const long DOUBLE_CLICK_INTERVAL = 2000000;
public DCCanvas() : base(){
doubleClickStarted = false;
doubleClickTimer = new DispatcherTimer();
doubleClickTimer.Interval = new TimeSpan(DOUBLE_CLICK_INTERVAL);
doubleClickTimer.Tick += new EventHandler(doubleClickTimer_Tick);
MouseUp += new MouseButtonEventHandler(mouseUpReaction);
}
private void mouseUpReaction(object sender, MouseButtonEventArgs e){
if(doubleClickStarted) {
doubleClickStarted =false;
if(MouseDoubleClick!=null)
MouseDoubleClick(sender, e);
}
else{
doubleClickStarted =true;
doubleClickTimer.Start();
}
}
private void doubleClickTimer_Tick(object sender, EventArgs e){
doubleClickStarted = false; doubleClickTimer.Stop();
}
}
}
Here above is my Canvas class. You can use it to make it simple to handle double clicks on Your Canvas. It will fire with every second mouseUp in specified interval (const DOUBLE_CLICK_INTERVAL in code). Seems to me not very hard to use:
上面是我的 Canvas 类。您可以使用它来轻松处理您的画布上的双击。它将在指定的时间间隔内每秒触发一次 mouseUp(代码中的 const DOUBLE_CLICK_INTERVAL)。在我看来不是很难使用:
public partial class MainWindow : Window
{
DCCanvas rusCanvas1;
public MainWindow(){
InitializeComponent();
rusCanvas1 = new DCCanvas();
/* Some code with properties for new rusCanvas */
this.grid1.Children.Add(rusCanvas1);
rusCanvas1.MouseDoubleClick += new MouseButtonEventHandler(canvas1_doubleClick);
}
private void canvas1_doubleClick(object sender, MouseButtonEventArgs e) {
MessageBox.Show(sender.ToString());
}
}
If you don't want to write properties for the Canvas, you can add a constructor-copyer to the class:
如果不想为 Canvas 编写属性,可以向类中添加构造函数复制器:
public DCCanvas(Canvas source) : base(){
Margin = source.Margin;
Style = source.Style;
Height = source.Height;
Width = source.Width;
Background = source.Background;
VerticalAlignment = source.VerticalAlignment;
HorizontalAlignment = source.HorizontalAlignment;
doubleClickStarted = false;
doubleClickTimer = new DispatcherTimer();
doubleClickTimer.Interval = new TimeSpan(DOUBLE_CLICK_INTERVAL);
doubleClickTimer.Tick += new EventHandler(doubleClickTimer_Tick);
MouseUp += new MouseButtonEventHandler(mouseUpReaction);
}
Oky, now You can create Your canvas in visual editor and then write like this:
好的,现在你可以在可视化编辑器中创建你的画布,然后像这样写:
rusCanvas1 = new DCCanvas(c1);
// "c1" is a name of your pre-created standard Canvas.
this.grid1.Children.Add(rusCanvas1);
this.grid1.Children.Remove(c1);
Just copy it and delete old one.
只需复制它并删除旧的。
I wrote this class, as I want this feature to be available as a standart. I mean, Canvas(and all other controls and objects!) must be able to handle double clicks on it without additional coding...
我编写了这个类,因为我希望此功能可以作为标准使用。我的意思是,画布(以及所有其他控件和对象!)必须能够在没有额外编码的情况下处理双击...
回答by Yoda
Sorry for being late to the party, but I'd like to suggest that you're better off with the first answer (by Jaster) to Why doesnt WPF border control have a mousedoubleclick event?.
抱歉迟到了,但我想建议您最好使用第一个答案(由 Jaster)回答为什么 WPF 边框控件没有 mousedoubleclick 事件?.
It's way more cleaner and doesn't even use one single line of code behind, hence it's fully MVVM-compliant and should be your way to go.
它更简洁,甚至不使用一行代码,因此它完全符合 MVVM 并且应该是您要走的路。
<Window x:Class="Yoda.Frontend.MainView" x:Name="MainViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border>
<Border.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding YourBindableCommand}"
CommandParameter="{Binding}" />
</Border.InputBindings>
</Border>
</Window>
Note:Of course, you have to replace YourBindableCommandwith the appropriate command, probably provided by your ViewModel. If you need help on that, just let me know.
注意:当然,您必须替换YourBindableCommand为适当的命令,可能由您的 ViewModel 提供。如果您需要这方面的帮助,请告诉我。
回答by kenny
Both the Window, UserControl and it seems many/all Control based elements now support a MouseDoubleClick Event. I'm using VS2017 and buliding to .net 4.7.1, but maybe its older than that.
Window、UserControl 和似乎许多/所有基于 Control 的元素现在都支持 MouseDoubleClick 事件。我正在使用 VS2017 并构建到 .net 4.7.1,但可能比这更旧。

