A user on the Blend/SketchFlow forums asked a question where the answer is to use routed events in WPF. Here is the trivial example for that question.
The child control that raises the event:
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.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for ChildControl.xaml
/// </summary>
public partial class ChildControl : UserControl
{
public ChildControl()
{
this.InitializeComponent();
}
public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
"Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ChildControl));
// Provide CLR accessors for the event
public event RoutedEventHandler Tap
{
add { AddHandler(TapEvent, value); }
remove { RemoveHandler(TapEvent, value); }
}
private void SignalParent(object sender, System.Windows.RoutedEventArgs e)
{
this.RaiseEvent(new RoutedEventArgs(TapEvent, this));
}
}
}
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication4.ChildControl" x:Name="UserControl" UseLayoutRounding="True" d:DesignWidth="268" d:DesignHeight="143"> <Grid x:Name="LayoutRoot"> <Rectangle Fill="Red" Stroke="Black"/> <Button Content="Button" HorizontalAlignment="Left" Margin="62,32,0,67" Width="71" Click="SignalParent"/> </Grid> </UserControl>
The parent control that consumes the event:
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.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for ParentControl.xaml
/// </summary>
public partial class ParentControl : UserControl
{
public ParentControl()
{
this.InitializeComponent();
}
private void HandleChildSignal(object sender, System.Windows.RoutedEventArgs e)
{
this.Text.Text = "The Child control made this event happen.";
}
}
}
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication4" mc:Ignorable="d" x:Class="WpfApplication4.ParentControl" x:Name="UserControl" UseLayoutRounding="True" d:DesignWidth="437" d:DesignHeight="325"> <Grid x:Name="LayoutRoot"> <Rectangle Fill="Blue" Stroke="Black"/> <local:ChildControl Margin="61,87,108,95" Tap="HandleChildSignal"/> <TextBlock x:Name="Text" Height="39" Margin="51,19,135,0" TextWrapping="Wrap" Text="This is the original text" VerticalAlignment="Top" Foreground="White" FontSize="24"/> </Grid> </UserControl>
Usage of the parent class:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication4" x:Class="WpfApplication4.MainWindow" x:Name="Window" Title="MainWindow" UseLayoutRounding="True" Width="640" Height="480"> <Grid x:Name="LayoutRoot"> <local:ParentControl Margin="30,33,157,84"/> </Grid> </Window>
February 12th, 2011 on 2:13 pm
Thanks … I saw about four definitions and had trouble piecing it together.
Thanks.
August 11th, 2011 on 3:28 am
Hey… can i also use frames insted of just placing “<local:ChildControl…" ?
August 18th, 2011 on 8:42 am
Every other example I have seen does the whole thing in one single class, which is just unbelievable. Why would you demonstrate routed events and only show it all going on in one single class??? Thats what I hate about WPF. Its so hard to learn and nearly all of the tutorials on it are so dumb and lazy.
This one is good.