RoutedEvent vs AttachedEvent
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: RoutedEvent vs AttachedEvent

Threaded View

  1. #1
    Join Date
    Feb 2001
    Posts
    872

    RoutedEvent vs AttachedEvent

    Hello, I've managed to setup an example working for both RoutedEvent and AttachedEvent but it appears the difference between the two is just syntactical as "RoutedEvent" handler can be declared in XAML in same syntax as "AttachedEvent".

    I understand phylosophical difference between DependencyProperty and AttachedProperty. I understand perfect "Grid.Row" is an "Attached Property" that it's not a property of "TextBox" itself for instance. I understand analogy when this concept extended to "Events". However - what's confusing is the SYNTAX: you can define an event handler to a RoutedEvent the same way/syntax as you would with AttachedEvent.

    Here's an example, I have MainWindow.xaml the top level form, containing user control MiddleChild.xaml, which in turn contain GrandChild.xaml (where one "RoutedEvent" and one "AttachedEvent" is declared)
    Code:
    public partial class GrandChild : UserControl
        {
            #region Routed Event
            public static readonly RoutedEvent GrandChildRoutedEvClickEvent = EventManager.RegisterRoutedEvent("GrandChildRoutedEvClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(GrandChild));
    
            // Provide CLR accessors for the event
            public event RoutedEventHandler GrandChildRoutedEvClick
            {
                add { AddHandler(GrandChildRoutedEvClickEvent, value); }
                remove { RemoveHandler(GrandChildRoutedEvClickEvent, value); }
            }
            #endregion
    
            #region Attached Event
            public static readonly RoutedEvent GrandChildAttachedEvClickEvent =
                EventManager.RegisterRoutedEvent("GrandChildAttachedEvClick",
                                                RoutingStrategy.Bubble,
                                                typeof(RoutedEventHandler),
                                                typeof(GrandChild));
    
            public static void AddGrandChildAttachedEvClickHandler(DependencyObject o, RoutedEventHandler handler)
            {
                ((UIElement)o).AddHandler(GrandChild.GrandChildAttachedEvClickEvent, handler);
            }
            public static void RemoveGrandChildAttachedEvClickHandler(DependencyObject o, RoutedEventHandler handler)
            {
                ((UIElement)o).RemoveHandler(GrandChild.GrandChildAttachedEvClickEvent, handler);
            }
            #endregion
    
            public GrandChild()
            {
                InitializeComponent();
            }
    
            private void btnGrandChild_Click_1(object sender, RoutedEventArgs e)
            {
                this.RaiseEvent(new RoutedEventArgs(GrandChildRoutedEvClickEvent, this));
                this.RaiseEvent(new RoutedEventArgs(GrandChildAttachedEvClickEvent, this));
                return;
            }
        }
    Now, from MiddleChild.xaml, I declare handler for "RoutedEvent" in xaml as follows:
    Code:
    <UserControl x:Class="RoutedEventDemo.MiddleChild"
                 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" 
                 xmlns:local="clr-namespace:RoutedEventDemo"
                 mc:Ignorable="d" 
                 Background="DarkGray"
                 
                 d:DesignHeight="300" d:DesignWidth="300">
        <StackPanel>
            <Label>MiddleChild</Label>
            <local:GrandChild x:Name="Junior" VerticalAlignment="Stretch" GrandChildRoutedEvClick="GrandChild_GrandChildRoutedEvClick"></local:GrandChild>
        </StackPanel>
    </UserControl>
    All good so far. Now, here comes the confusing part, in MainWindow.xaml - I use the same syntax to setup event handler for both "RoutedEvent" and "AttachedEvent" - this lead me to the question, really what's difference between RoutedEvent and AttachedEvent?!
    Code:
    <Window x:Class="RoutedEventDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:RoutedEventDemo"
            xmlns:dotnet="clr-namespace:System.Windows.Controls"
            Title="MainWindow" Height="350" Width="525" Background="DimGray"
            
            local:GrandChild.GrandChildRoutedEvClick="GrandChild_GrandChildRoutedEvClick"
            >
        <StackPanel local:GrandChild.GrandChildAttachedEvClick="GrandChildAttachedEvClickHandler" >
            <Label>Main Window</Label>
            <local:MiddleChild></local:MiddleChild>
        </StackPanel>
    </Window>
    REF:
    http://en.csharp-online.net/WPF_Conc...Implementation
    http://wpftutorial.net/RoutedEvents.html
    Last edited by THY02K; January 30th, 2013 at 05:07 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center