WPF窗口边框上的双击事件是否会触发任何事件?
我怎么能抓住它?
谢谢!
答案 0 :(得分:38)
这是一种方法..只需将Window.WindowStyle设置为“None”并创建下窗口边框:
<Grid>
<Border
BorderBrush="Silver"
BorderThickness="10"
Name="border1"
MouseLeftButtonDown="border1_MouseLeftButtonDown" />
</Grid>
在代码背后:
private void border1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
MessageBox.Show("Double Click");
}
答案 1 :(得分:11)
很抱歉迟到了,但我想建议你最好用第一个答案(Jaster)到Why doesnt WPF border control have a mousedoubleclick event?。
它更清洁,甚至不使用一行代码,因此它完全符合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>
注意:当然,您必须使用ViewModel提供的相应命令替换YourBindableCommand
。如果您需要帮助,请告诉我。
答案 2 :(得分:1)
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();
}
}
}
以上是我的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());
}
}
如果您不想为Canvas编写属性,可以在类中添加构造函数 - copyer:
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,现在你可以在可视化编辑器中创建你的画布,然后像这样写:
rusCanvas1 = new DCCanvas(c1);
// "c1" is a name of your pre-created standard Canvas.
this.grid1.Children.Add(rusCanvas1);
this.grid1.Children.Remove(c1);
只需复制并删除旧的。
我写了这个课程,因为我希望这个功能可用作标准。我的意思是,Canvas(以及所有其他控件和对象!)必须能够在没有额外编码的情况下对其进行双击...
答案 3 :(得分:0)
Window,UserControl和许多基于控件的元素现在都支持MouseDoubleClick事件。我正在使用VS2017,并扩展到.net 4.7.1,但也许比它更旧。