Click here to Skip to main content
16,020,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to create double click event to dynamic canvas
how can i do that?


C#
 private void AddItem_Click(object sender, RoutedEventArgs e)
        {           
     
            Grid gridborder = new Grid()
            {
               Width=80,Height=150

            };

         
            Canvas piCanv = new Canvas()
            {
                ToolTip="canvas",
                Width = 75,
                Height = 120,
                Margin=new Thickness(1,40,1,1),
                Background=new SolidColorBrush(Colors.LightCyan ),

            };
// her i want to create event to the dynamic canvas double click event
            
            if (piCanv == MouseAction.LeftClick)
            {}

           
          
        }
Posted
Comments
Sergey Alexandrovich Kryukov 3-Dec-13 9:11am    
You are not trying to create an event. At best, you are trying to add a handle to the invocation list of existing event instance. But this would be += operator which you did not even show. You need to explain what you are really trying to achieve, exactly, and what is the problem.
Better first read how delegates and events work in a standard MSDN reference and manual.
—SA

1 solution

Double click event. you can change the interval time.
C#
piCanv.PreviewMouseLeftButtonUp += new  MouseButtonEventHandler(Canvas_PreviewMouseLeftButtonUp);
int priviewTimestamp = -1;
const int iDoubleClickInterval = 500;
private void Canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (priviewTimestamp > 0)
    {
        if (e.Timestamp - priviewTimestamp < iDoubleClickInterval)
        {
            //this is the double click event
            //raise the double click event here or handle it here
        }
        priviewTimestamp = e.Timestamp;
    }
    else
        priviewTimestamp = e.Timestamp;
}
 
Share this answer
 
Comments
Leung Yat Chun 5-Dec-13 2:27am    
Or better yet, use MouseButtonEventArgs.ClickCount.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900