Search code examples
silverlightsilverlight-4.0mouseeventmouseclick-event

Mouse Click in Silverlight 4


I need a mouse click event in my silver light project and I know we need to simulate it ourself if the object is not button. lets say I want mouseclick for my img... How exactly can we track time between mousedown and mouseup and say if the time between them is less than 300m, we have a mouse click?


Solution

  • Handle the MouseLeftButtonDown and MouseLeftButtonUp events for your image.

    private DateTime? startClick;
    
    private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        startClick = DateTime.Now;
    }
    
    private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        var clickDuration = DateTime.Now - startClick;
    
        if (startClick != null && clickDuration < TimeSpan.FromMilliseconds(300))
        {
            MessageBox.Show("Less than 300ms!");
        }
    
        startClick = null;
    }