21

Is there any event which fires on double-click event on WPF Window border?

How I can catch it?

Thanks!

7
  • 1
    Maybe this will help - stackoverflow.com/questions/3410768/…
    – Bali C
    Feb 9, 2012 at 14:52
  • @BaliC Sorry, I don't understand how I can do it for the simple WPF Window?
    – Terminador
    Feb 9, 2012 at 14:55
  • You mean double click on window chrome? The same double click that causes maximize/minimize of the window? Feb 9, 2012 at 14:58
  • @ppiotrwicz Yes I mean a window border. I need the event to maximize/minimize Window
    – Terminador
    Feb 9, 2012 at 15:00
  • 1
    if you're using system chrome - system is managing the maximizing and minimizing of the window. You can listen to Window.SizeChanged event and check Window.WindowState property in it's handler. Although it's not a perfect solution :) Feb 9, 2012 at 15:10

4 Answers 4

56

Here is one way.. Just set your Window.WindowStyle to "None" and create your down window border:

<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");
}
3
  • Thanks! But I am talking about native Window border... and you suggest to use WPF Border control...
    – Terminador
    Feb 9, 2012 at 15:05
  • I suggested that one way to attack was to draw your own border, then you would have full control over the events.
    – RQDQ
    Feb 9, 2012 at 15:13
  • Ahhh Then yes it could be a solution. Thank you!
    – Terminador
    Feb 9, 2012 at 15:45
22

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?.

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.

<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. If you need help on that, just let me know.

2
  • 4
    To whoever voted down my improved answer: please either provide feedback on what's (still) not enhanced enough or remove the -1. Everything else is just useless as it won't help anyone.
    – Yoda
    Oct 28, 2016 at 12:37
  • Brilliant answer - I missed <InputBindings> feature somehow. What is important most - it works for the mouse events performed on any child element inside the Border (or StackPanel in my case) Dec 11, 2020 at 14:11
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();
        }
    }
}

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:

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:

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...

0

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.