Posted by: Zeeshan Amjad | December 29, 2009

Using Event Setter class


Event setter class is inherited by SetterBase class just like the Setter class. These classes are used to define the style of the program. Setter class is used to set the properties, on the other hand event setter class is used to set the event handler of the control. Here is a class diagram of Event setter class.

SetterBase

Here is a simple program to show the usage of event setter class.

  1: <Window x:Class="WpfEventStyle.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Event Setter" Height="300" Width="300">
  5:     <Window.Resources>
  6:         <Style TargetType="Button">
  7:             <Setter Property="Margin" Value="5"/>
  8:             <Setter Property="FontWeight" Value="Bold"/>
  9:             <Setter Property="Foreground" Value="Blue"/>
 10:             <EventSetter Event="Click" Handler="btnClick"/>
 11:         </Style>        
 12:     </Window.Resources>
 13:     <StackPanel>
 14:         <Button Name="btn1">Button 1</Button>
 15:         <Button Name="btn2">Button 2</Button>
 16:         <Button Name="btn3">Button 3</Button>
 17:         <Button Name="btn4">Button 4</Button>
 18:     </StackPanel>
 19: </Window>
 20: 

Here is the C# code of this program.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace WpfEventStyle
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public Window1()
 23:         {
 24:             InitializeComponent();
 25:         }
 26: 
 27:         void btnClick(object sender, RoutedEventArgs e)
 28:         {
 29:             MessageBox.Show("Click On Button", "Event Setter");
 30:         }
 31:     }
 32: }
 33: 

This is the output of this program.

EventSetterOutput

This program will display the message box when user click on any button with message box “Click on Button” and “Event Setter” caption.

Advertisements

Responses

  1. hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I

    was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful

    for .Net community.

    There is a good C# resource site, Have alook

    http://www.csharptalk.com/2009/09/c-class.html
    http://www.csharptalk.com

    simi


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: