Blog

Events in WPF

21 September, 2018

Related Blogs

Events

The events associated with any control or window in WPF are known as Rooted events WPF supports 3 – types of rooted events  
  1. Direct Events
  2. Bubbling Events
  3. Tunneling Events
  Direct Events:- These are coursed directly when user clicks on the particular control
  1. Click event of a button
  2. Checked changed button of a radio button or checkbox
  • Selected index changed event of a list box etc
  Bubbling Events:- Caving the events in the order of child controls to its parent controls is known as event bubbling EG: Mouse down, Mouse up etc   Tunneling Events:- Calling the events in the order of parent control to its child controls is known as tunneling events Eg:- Preview mouse down, Preview mouse up etc   Eg  with Bubbling Events:- Select the label Screenshot_1 Go to properties window

       ↓

Click on events button

       ↓

Double click on Mouse down events

       ↓

Write the following code Private void label – Mouse down (object sendes, mouse button event Args) { Message Box.show (‘Label”) } Switch to .xaml file design

       ↓

Select the window1

       ↓

Go to properties window

       ↓

Click on event button

       ↓

Double click on mouse down event

       ↓

Write the following code.  
  • Private void window1_Mouse Design (object sender mouse button event args e)
{ Message Box show (“window”), } Run the application click on label and check
  • First we get message label then window
  • Here event firing is flowing from child control e label to parent control window
  • This is known as event bubbling and the events are called bubbling events

Example  With Tunneling Events

Steps to add a new window to the application

       ↓

Go to Solution explores

       ↓

Select the Solution

       ↓

click on sight mouse button

       ↓

Click on add

       ↓

Click on window

       ↓

window name

  ↓

Type

   ↓

click on add   Create a label and write the following code 5 preview mouse down event of label and window private void label_ Preview mouse down (object sender, mouse down event args e) { message box show (“label”) } Private void window2 _ Preview Mouse Down (object send Mouse down event Args e) { Message Box show (“window”); }  

Steps to change startup object

Go to solution explores Double click on app. XML (This file is known as startup file in WPF) Change start up rui like.
  • Startup uri= “window2.xam.”
Run the application & chact
  • When we click on label first we get a message window then label
  • Here event firing is following from parent control to child control
This is known as Event Tunneling and the events our known as Tunneling events. Window Life Time in WPF:- A window lifetime in WPF starts when it is instantiated, after which it is opened, activated can be deactivated  and then closed window lifetime will be ended when it is  closed nothing but when its object is destroyed.  

Window lifetime events:-

Screenshot_1   Eg to open and close a window from other windows:- Create a new WPF application

           ↓

create two windows with the names window1, window2   3   Window2   object= new                window2():   [Design the window1, create3 buttons and name as  btnopen, btnclose btntide, write the following code.] Private void btn open_click(--) { Objt.show(); } Private void btnhide_click() { obj_hide(); } The private void btnclose_click(--) { Obj%close(); } Run the apps and check When user clicks on hide object will not be destroyed and next time user clicks on open window will  be displayed without any exception but when user clicks on close object will be destroyed and next time when user clicks on open it raises exception.   Properties of window object:- 11Back ground                                            11show in task bar. 11Border brush                                          11Size To content 11Border thickness                                   11Title 11Icon                                                            11Top Most 11IsEnabled                                                 11Window startup location 11Window state
  • Background:- Used to set or get background color to the window
  • Border brash:- Used to set or get the required color to the border of the window
  • Border thickness:- Used to set or get the thickness value to the border
  • Icon:- used to set or get the required Icon file that is to be displayed at the icon location of the window
  • Is Enabled when set to True:- The user can access the window at runtime False:-  Icon image will not appear within the taskbar
  • Show Task Bas
True: (Default) the icon image of the window will appear within the taskbar False  : Icon image will not appear within the taskbar   Screenshot_2       Manual : When set to Manual window will have its own size. According to the contents of the window size will be increased or decreased   Height:- The Window will be scaled to content height   Width:The window width will be scaled to content width   Height & width:- Widow height & width will be scaled to contents height & width
  • Top Most:-
True:- When set to true the window will be within the topmost of all other windows False:-  The window will not be the topmost one in all opened windows
  • Window start up location:- Used to set or get the required location within window when it is opened at runtime.
  • Window state:- When set to normal Window will be opened with its default size
  Minimized:- Opened in minimized form   Maximized:- Window opened in maximized form