2012-05-19

Articles

EventArgs

 

Every application uses custom events, and in the most cases any kind of parameter has to pass with. To do this a new object derived from the EventArgs have to create. This is a generic representation of such an EventArgs, you can pass any kind of parameter with it.

 

alt

 

Usage

<UserControl x:Class="DW.SharpTools.Demo.EventArgsControl"
			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
			 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
			 mc:Ignorable="d" 
			 d:DesignHeight="300" d:DesignWidth="300">
	<StackPanel>
		<TextBox x:Name="message" />
		<Button Margin="5" Content="Send" Click="Send_Click" HorizontalAlignment="Center" />
	</StackPanel>
</UserControl>
using System;
using System.Windows.Controls;

namespace DW.SharpTools.Demo
{
	public partial class EventArgsControl : UserControl
	{
		public EventArgsControl()
		{
			InitializeComponent();
		}

		private void Send_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			var handler = Message;
			if (handler != null)
				handler(this, new EventArgs<string>(message.Text));
		}

		public event EventHandler<EventArgs<string>> Message;
	}
}