tags:

views:

225

answers:

4

Hello All

i want to FIre the button Click event When My Window is Loaded.. How Can i Achieve it in Wpf.

thanks in advance

shashank

+3  A: 

Create a single function with the shared behavior in your window, then call that function from both your loaded handler and your click handler.

Greg D
A: 

in you page_loaded event handler method, make a call to the click event like this:

_buttonName_click(sender, new RoutedEventArgs())

VoodooChild
To clarify, this isn't actually calling the 'event'... It's calling the method (which happens to also be a handler for the click event). But yes, this is correct way to do it if we know this is the only handler we need to call to.
Reddog
+1 Thanks! I edited my answer!
VoodooChild
A: 

You could use Automation to do it aswell - I've seen this suggested some places as the more flexible/robust method to use, but it seems a bit heavy weight to me compared to just calling the method you already have directly.

Greg Domjan
+1  A: 

As per this blog post in WinForms this was really easy by just calling PerformClick(), but in WPF you can do it with Automation, however as a commenter mentioned it's really easy if you have access to the button to just use RaiseEvent.

someButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

But as previously answered, if you only have a single handler that needs to be notified, then simply call that handler method directly.

Reddog