Event sender, event source and event original source : Event « Windows Presentation Foundation « C# / C Sharp






Event sender, event source and event original source

Event sender, event source and event original source
   

<Window x:Class="RoutedEvents.BubbledLabelClick"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BubbledLabelClick" Height="360" Width="330" MouseUp="SomethingClicked" >
    <StackPanel MouseUp="SomethingClicked">
      <Label Margin="5" BorderThickness="1" MouseUp="SomethingClicked" HorizontalAlignment="Left" >
        <StackPanel MouseUp="SomethingClicked" >
          <TextBlock Margin="3" MouseUp="SomethingClicked">label</TextBlock>
          <Image Source="c:\image.jpg" Stretch="None" MouseUp="SomethingClicked" />
          <TextBlock Margin="3" MouseUp="SomethingClicked">test</TextBlock>
        </StackPanel>
      </Label>
      <ListBox Margin="5" Name="lstMessages" Grid.Row="1"></ListBox>
      <CheckBox Margin="5" Name="chkHandle">Handle first event</CheckBox>
      <Button Click="cmdClear_Click" HorizontalAlignment="Right" Margin="5" Padding="3">Clear List</Button>
    </StackPanel>
</Window>
//File:Window.xaml.cs


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RoutedEvents
{
    public partial class BubbledLabelClick : System.Windows.Window
    {

        public BubbledLabelClick()
        {
            InitializeComponent();
        }

        protected int eventCounter = 0;

        private void SomethingClicked(object sender, RoutedEventArgs e)
        {            
            eventCounter++;
            string message = "#" + eventCounter.ToString() + ":\r\n" + 
                " Sender: " + sender.ToString() + "\r\n" +
                " Source: " + e.Source + "\r\n" +
                " Original Source: " + e.OriginalSource;
            lstMessages.Items.Add(message);
            e.Handled = (bool)chkHandle.IsChecked;            
        }

        private void cmdClear_Click(object sender, RoutedEventArgs e)
        {            
            eventCounter = 0;
            lstMessages.Items.Clear();
        }
    }
}

   
    
    
  








Related examples in the same category

1.Cast event sender to a controlCast event sender to a control
2.Press F1 to get helpPress F1 to get help
3.Raise the PropertyChanged eventRaise the PropertyChanged event
4.Get the event sender nameGet the event sender name
5.Use Ellipse event delegateUse Ellipse event delegate
6.Halting event routing with HandledHalting event routing with Handled
7.Use Ellipse.AddHandler to add handler to Ellipse objectsUse Ellipse.AddHandler to add handler to Ellipse objects
8.Get event sender from eventGet event sender from event
9.Handle the ContentRendered eventHandle the ContentRendered event
10.Find source element of an element in event handler by casintFind source element of an element in event handler by casint
11.Add an event handler to an element using codeAdd an event handler to an element using code
12.Handles the Click event on the UniformGridHandles the Click event on the UniformGrid
13.Mark the text control as being changed to prevent any text content or selection changed eventsMark the text control as being changed to prevent any text content or selection changed events
14.Check handler event senderCheck handler event sender
15.Generate Click Events Repeatedly While a Button Is ClickedGenerate Click Events Repeatedly While a Button Is Clicked
16.Raise the ScrollChanged event of a ScrollViewerRaise the ScrollChanged event of a ScrollViewer
17.Event firing sequenceEvent firing sequence
18.ToolBar and event handlerToolBar and event handler
19.Cancel event by setting CanExecute and Handled to falseCancel event by setting CanExecute and Handled to false
20.Activated and Deactivated events monitors the activation status of an application.
21.Attach a double click to an object