Double-click event?

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
tomny
Posts: 38
Joined: Sat Nov 19, 2016 7:23 pm

Double-click event?

Postby tomny » Sun Jan 15, 2017 2:53 am

Dear JS-Support,

I want a controls (Image, TextBlock ...) to achieve a double-click event, how to achieve it?

Thank you.
Regards,
Tomny

JS-Support @Userware
Site Admin
Posts: 1093
Joined: Tue Apr 08, 2014 3:42 pm

Re: Double-click event?

Postby JS-Support @Userware » Wed Jan 18, 2017 7:32 am

Dear Tomny,

Sure!

Here is how you can add a DoubleClick event to an existing control.

Let's create a TextBlock2 class that inherits from TextBlock:

Code: Select all

public class TextBlock2 : TextBlock
{
    private DateTime _lastPointerPressed = DateTime.Now;
    public event PointerEventHandler DoubleClick;

    public TextBlock2()
    {
        this.PointerPressed += MainPage_PointerPressed;
    }

    void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        DateTime now = DateTime.Now;
        if ((now - _lastPointerPressed).TotalMilliseconds <= 300)
        {
            if (DoubleClick != null)
            {
                DoubleClick(this, e);
            }
            e.Handled = true;
        }
        _lastPointerPressed = now;
    }
}


To test it, just declare it somewhere in your xaml code:

Code: Select all

<local:TextBlock2 Text="Double-click me to test" DoubleClick="TextBlock2_DoubleClick"/>


and use the following code-behind:

Code: Select all

void TextBlock2_DoubleClick(object sender, PointerRoutedEventArgs e)
{
    MessageBox.Show("Double-click works!");
}


Regards,
JS-Support


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 3 guests

 

 

cron