Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-click event on WPF Window border

Tags:

c#

.net

wpf

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

How I can catch it?

Thanks!

like image 835
Terminador Avatar asked Feb 09 '12 14:02

Terminador


3 Answers

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");
}
like image 69
RQDQ Avatar answered Nov 04 '22 06:11

RQDQ


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.

like image 42
Yoda Avatar answered Nov 04 '22 04:11

Yoda


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

like image 1
frosteroid Avatar answered Nov 04 '22 04:11

frosteroid