views:

197

answers:

3

In Silverlight 4, I wish to invoke one of the mouse button click events when the right mouse button is clicked. I have the RightMouseButtonDown click event wired up, but I don't know how to manually fire the MouseLeftButtonUp event.

I have tried raising the event in the following fashion.

private void MainLayoutRootMouseButton(object sender, MouseButtonEventArgs e)
{
    MouseLeftButtonDown(sender, e);
}

However, a compiler error occurs:

"The event 'System.Windows.UIElement.MouseLeftButtonDown' can only appear on the left hand side of += or -=

What do I need to do to manually raise this event?

+1  A: 

You really shouldn't be raising the event itself. Instead, make the code inside the MouseLeftButtonUp its own function and then call that function from both the MouseLeftButtonUp and MouseRightButtonUp events.

e.g.

private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DoTheSameThing(sender, e);
}

private void MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    DoTheSameThing(sender, e);
}

private void DoTheSameThing(object sender, MouseButtonEventArgs e)
{
    //Handle left or right mouse buttons up event
}
Chad
A: 

In your page's init function you need to have this line of code

someObjectOnPage.MouseLeftButtonDown += new MouseDownEventHandler(DoSomething);
someObjectOnPage.MouseRightButtonDown += new MouseDownEventHandler(DoSomething);

This is pure psuedo code but should lead you on the right track

also your method will need to look like this

void DoSomething(object sender, MouseButtonEventArgs e) { }
Jimmy
+1  A: 

The other answers are correct in that they show you how to call those same code paths as you have for the other mouse events.

It should be clear that in Silverlight, you cannot raise (or automate) actual mouse button clicks, for security reasons.

Only user initiated actions such as the actual mouse moving can create real MouseEventArgs and fire the handlers directly, through the Silverlight input system.

Jeff Wilcox
Given this information, it looks like I'll just have to call the code that LeftMouseButtonUp is calling when the right mouse button is clicked.
Secret Agent Man