Question

WPF, Shapes

I have a custom Shape, and try to implement MouseClick and MouseDoubleClick stuff on it.

By eg. I need to open a information window about MyShape OnClick and select it OnDoubleClick.

The stub is the following:

public class MyShape : Shape
{
    public Point Point1, Point2;

    public MyShape() : base() { }

    protected override Geometry DefiningGeometry
    {
        get { return new LineGeometry(Point1, Point2); }
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        if (e.ClickCount == 1)
        {
            // Open Informational Window
        }
        else if (e.ClickCount == 2)
        {
            // Select Item
        }
    }
}

Every time I doubleclick both // Only do MouseClick and // Only do MouseDoubleClick happens.

Is there any way to avoid this in WPF, .NET 4?

Was it helpful?

Solution

Between first click and second click in double click I think there is a time around 50 and 400 millisecond, So the bellow code is not a best one but do the trick:

bool firstClicked = false;

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        if (e.ClickCount == 1)
        {
            firstClicked = true;
            Task tsk = new TaskFactory()
                            .StartNew(
                            () => 
                                {
                                    Thread.Sleep(SystemInformation.DoubleClickTime);
                                    if (firstClicked)
                                    {
                                        // if you want to use `e`
                                        // e.Source
                                        // Open Informational Window
                                    }
                                        firstClicked = false;
                                    }
                                        );

                                }

        }
        else if (e.ClickCount == 2)
        {
            firstClicked = false;
            // Select Item
        }
    }

OTHER TIPS

As I said in a comment, I once had this very same problem. I needed to detect double clicks, but clicks and double clicks should behave as separate events. Meaning, if a double click was fired, click should not fire (because each action had different and not compatible effects in the UI).

The "native" double click didn't work for me because click events fired whether a double click happened or not. Here's some sample code in Actionscript. I'm sure you'll be able to follow the (simple) logic to implement it in .net. Note that I'm hardcoding the double click time, as I couldn't get the system value for it from Actionscript. It's not ideal, but it's I compromise I had to make. You should use the time interval provided by the OS (or your runtime) instead, if it's available to you.

Hope it helps.

package {
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;

    public class Button extends Sprite {

        public function DCButton() {
            super();
            graphics.beginFill(0xff0000);
            graphics.drawRect(0,0,100,100);
            graphics.endFill();
            addEventListener(MouseEvent.CLICK,handleClick);
        }

        private var _clickCount:int = 0;
        private var _doubleClickInterval:int = 200;

        private function handleClick(e:MouseEvent):void {
            _clickCount++;
            //  start the timer and when it completes, check the number of clicks
            //  based on this, dispatch either click or doubleClick events
            if(_clickCount == 1) {
                setTimeout(checkDoubleClick,_doubleClickInterval,e);
            }
            else
            {
                checkDoubleClick(e);
            }

        }   

        private function checkDoubleClick(e:MouseEvent):void {

            if(_clickCount == 0) return;

            var type:String;

            if(_clickCount == 1) {
                type="click";
            } else if(_clickCount > 1) {
                type="doubleClick";
            }

            trace(type);

            _clickCount = 0;
            //  dispatch the detected event here...

        }           
    }


}

Every DoubleClick starts with a Click.

maybe try an attached behaviour?

Try this link for an explanation

Maybe you can aggregate the events...i think that is what you are trying to do, right?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top