views:

2044

answers:

8

There is the button control in silverlight application . Can I send a mouse click event to it programmatically?

A: 

I've not used Silverlight but I assume it's the same process as Windows.Forms and WebControls. You'll just need to call the button's .Click(Object o, EventArgs e) method.

Oli
+1  A: 

You can't make the Click event fire for security reasons, because then you would be able to do things like force a user into full screen mode without them knowing it. As Oli said, you could call the Click event handler directly, but you can't actually fire a Click event.

Bill Reiss
A: 

Oli, when i try to call event handler directly I have that error: "The event 'System.Windows.Controls.Primitives.ButtonBase.Click' can only appear on the left hand side of += or -=".

A: 

I see, event delegate can be called from class where it is declared. But is there a way to call event delegate from other class?

A: 

The classic way to do this in .Net is to P/Invoke SendInput() from user32.dll, since there's no way to do this with the .Net framework.

I'm not familiar with Silverlight, but I know that it uses a compact sandbox of a .Net, so if interoperability is available, you'll find plenty examples on the internet.

John Smith
A: 

I have buttons for CRUD operations in my page, after Save, Delete or Update I need Refresh the Data in a Datagrid, The most easy way was to send click event to the "Read" Button, from the Others CRUD buttons

This code fire that event:

    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
  //.....Save Operation

      //--At Finish refresh the datagrid
      btnRead_Click(btnRead, new RoutedEventArgs());
    }
Damián Ulises Cedillo
A: 

Try using Automation Peers (if you absolutely need to do this programmatically).

http://www.vbdotnetheaven.com/UploadFile/dbeniwal321/TriggerEvent01232009020637AM/TriggerEvent.aspx has a sample using vb.net

Ideal way would be to have a shared function which gets called both from button click handler as well as other cases where needed

moonlightdock
+2  A: 

If you still want to do that. You can now upgrade to Silverlight version 3.0 or later versions and do the following:

You can use the button automation peer from System.Windows.Automation.Peers to accomplish what you want.

if (button is Button)
{
    ButtonAutomationPeer peer = new ButtonAutomationPeer((Button)button);

    IInvokeProvider ip = (IInvokeProvider)peer;
    ip.Invoke();
}
Nadzzz
+1: Perfect. Just what I was looking for. Cheers
Enough already