.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Basics of Event Handling in WPF

Posted By:Manas Patnaik       Posted Date: February 18, 2011    Points: 10    Category: WPF    URL: http://www.dotnetspark.com  

Basics of Event Handling in WPF. In this article I will show how handle event in WPF

Events in WPF/Silverlight works same as in Win Form application except some new concept such as  Routed Event .So directly we can jump to the code to add simple events to application as well as Window.Altough the bellow sample is based on WPF but it holds true for Silverlight also. 
There are 2 ways of adding events

  1. Declarative Way ( XAML Approach )
  2. Code Way (Conventional as in Win form / Code Approach)

So consider  the scenario bellow and the way to do it in both ways

Example 1 -: On Application Start ,display application details.


As we should on load of application the event must be declared in the app.XAML or app.xaml.cs.Write the the event name we want to invoke , the intellisense will prompt as follows


Once you click image , it will add a handler to the app.xaml.cs .And there we will have the application information.

  1. /// Interaction logic for App.xaml
  2. /// 
  3. public partial class App : Application
  4. {
  5. private void Application_Startup(object sender, StartupEventArgs e)
  6. {
  7. MessageBox.Show(Environment.Version + "\n" + Environment.UserName
  8. ,"My First WPF Application");
  9. }
  10. }

CODE Behind Way -: 

As the OnStartup is application related event we will override in the app.xaml.cs.


  1. protected override void OnStartup(StartupEventArgs e)
  2. {
  3. MessageBox.Show(Environment.Version + "\n" + Environment.UserName
  4. "My First WPF Application");
  5. }

Example 2 -: On Button Click display application information (Attaching a event to a method)

XAML Way -:

Create a EventHandaler in the the window , as bellow .

  1. public partial class MyFirstWindow : Window
  2. {
  3. public MyFirstWindow()
  4. {
  5. InitializeComponent();
  6. }
  7. private void ShowApplicationInfo(object sender,RoutedEventArgs e)
  8. {
  9. MessageBox.Show(Environment.Version + "\n" + Environment.UserName
  10. ,"My First WPF Application");
  11. }
  12. }

Now attach the event to the button click in through xaml .

 So the final Xaml code will look like this.

  1. <Grid>
  2. <Label>My First WPF ApplicationLabel>
  3. <Button Content="Show About" Height="50? HorizontalAlignment="Left" Margin="71,78,0,0?
  4. Name="btnShowAbout" VerticalAlignment="Top" Width="143?
  5. Click="ShowApplicationInfo"
  6. />
  7. Grid>

CODE Behind Way-:
Code way is same as previous we need to attach the event to the handler on load.

imageand the final code will be

  1. private void ShowApplicationInfo(object sender,RoutedEventArgs e)
  2. {
  3. MessageBox.Show(Environment.Version + "\n" + Environment.UserName
  4. ,"My First WPF Application");
  5. }
  6. private void Window_Loaded(object sender, RoutedEventArgs e)
  7. {
  8. btnShowAbout.Click += new RoutedEventHandler(ShowApplicationInfo);
  9. }

That's it .Hope it will help the newbies who have just started with WPF/Silverlight.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend