简体   繁体   English

双击WPF窗口边框上的事件

[英]Double-click event on WPF Window border

Is there any event which fires on double-click event on WPF Window border? WPF窗口边框上的双击事件是否会触发任何事件?

How I can catch it? 我怎么能抓到它?

Thanks! 谢谢!

Here is one way.. Just set your Window.WindowStyle to "None" and create your down window border: 这是一种方法..只需将Window.WindowStyle设置为“None”并创建下窗口边框:

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

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边境控制没有moused双击事件? .

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 YourBindableCommand with the appropriate command, probably provided by your ViewModel. 注意:当然,您必须使用YourBindableCommand提供的相应命令替换YourBindableCommand If you need help on that, just let me know. 如果您需要帮助,请告诉我。

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. 上面是我的Canvas类。 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). 它将在指定的时间间隔内每隔一个mouseUp触发一次(代码中为const DOUBLE_CLICK_INTERVAL)。 Seems to me not very hard to use: 在我看来不是很难使用:

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编写属性,可以在类中添加构造函数 - 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, now You can create Your canvas in visual editor and then write like this: 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);

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... 我的意思是,Canvas(以及所有其他控件和对象!)必须能够在没有额外编码的情况下对其进行双击...

Both the Window, UserControl and it seems many/all Control based elements now support a MouseDoubleClick Event. Window,UserControl和它似乎很多/所有基于控件的元素现在都支持MouseDoubleClick事件。 I'm using VS2017 and buliding to .net 4.7.1, but maybe its older than that. 我正在使用VS2017并建立.net 4.7.1,但可能比它更老了。

https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.control.mousedoubleclick?view=netframework-4.7.2 https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.control.mousedoubleclick?view=netframework-4.7.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM