locked
Event Handling with XAML & C# (Tunneling & Bubbeling) RRS feed

  • Question

  • I am a bit confused about working with tunneling an bubbeling in XAML and C#.


    I created a simple application to test Tunneling and Bubbeling in WPF:


    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="UntitledProject5.Window1"
        x:Name="Window"
        Title="Window"
        Width="300" Height="329">

         
       <Grid MouseDown="MouseDownGrid" PreviewMouseDown="PreviewMouseDownGrid">
        
           <Button HorizontalAlignment="Left" Margin="10,10,10,10" VerticalAlignment="Top" Width="250" Height="250" MouseDown="MouseDownButton" PreviewMouseDown="PreviewMouseDownButton">
         
               <Canvas Width="150" Height="150" Background="#FF5D5252" MouseDown="MouseDownCanvas" PreviewMouseDown="PreviewMouseDownCanvas">
           
                   <Ellipse Fill="#FFFF0000" Stroke="#FF000000" Width="100" Height="100" Canvas.Left="27" Canvas.Top="27" MouseDown="MouseDownEllipse" PreviewMouseDown="PreviewMouseDownEllipse"/>
               </Canvas>
           </Button>
      </Grid>
    </Window>


    With The corresponging Code Behind File for Event Handling:

    using System;
    using System.IO;
    using System.Net;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Navigation;

    namespace UntitledProject5
    {
        public partial class Window1
        {
            public Window1()
            {
                this.InitializeComponent();
               
                // Insert code required on object creation below this point.
            }

            private void MouseDownGrid(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("1-Ebene: MouseDownGrid");
                Debug.WriteLine("Handeled = " + e.Handled.ToString());
            }

            private void MouseDownButton(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("2-Ebene: MouseDownButton");
                Debug.WriteLine("Handeled = " + e.Handled.ToString());
            }

            private void MouseDownCanvas(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("3-Ebene: MouseDownCanvas");
                Debug.WriteLine("Handeled = " + e.Handled.ToString());
            }

            private void MouseDownEllipse(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("4-Ebene: MouseDownEllipse");
                Debug.WriteLine("Handeled = " + e.Handled.ToString());
            }

            //
            // Hier beginnt der Preview Mouse Down Bereich
            //

            private void PreviewMouseDownGrid(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("1-Ebene: PreviewMouseDownGrid");
                Debug.WriteLine("Handeled = " + e.Handled.ToString());
            }

            private void PreviewMouseDownButton(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("2-Ebene: PreviewMouseDownButton");
                Debug.WriteLine("Handeled = " + e.Handled.ToString());
            }

            private void PreviewMouseDownCanvas(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("3-Ebene: PreviewMouseDownCanvas");
                Debug.WriteLine("Handeled = " + e.Handled.ToString());
            }

            private void PreviewMouseDownEllipse(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("4-Ebene: PreviewMouseDownEllipse");
                Debug.WriteLine("Handeled = " + e.Handled.ToString());
                //e.Handled = true;
                //Debug.WriteLine(e.Source.ToString);
            }
        }
    }



    When i left Click the Ellipse Element i get this debug Code:

    1-Ebene: PreviewMouseDownGrid
    Handeled = False
    2-Ebene: PreviewMouseDownButton
    Handeled = False
    3-Ebene: PreviewMouseDownCanvas
    Handeled = False
    4-Ebene: PreviewMouseDownEllipse
    Handeled = False
    4-Ebene: MouseDownEllipse
    Handeled = False
    3-Ebene: MouseDownCanvas
    Handeled = False


    When i right click the Ellipse i get this debug Code:


    1-Ebene: PreviewMouseDownGrid
    Handeled = False
    2-Ebene: PreviewMouseDownButton
    Handeled = False
    3-Ebene: PreviewMouseDownCanvas
    Handeled = False
    4-Ebene: PreviewMouseDownEllipse
    Handeled = False
    4-Ebene: MouseDownEllipse
    Handeled = False
    3-Ebene: MouseDownCanvas
    Handeled = False
    2-Ebene: MouseDownButton
    Handeled = False
    1-Ebene: MouseDownGrid
    Handeled = False


    What is the Problem with the Code?
    Why is the right mouse click tunneling up and bubbeling down and the left mouse button not ?

    Thx for Help
    Friday, January 19, 2007 4:35 PM

All replies

  • Your tunneling events (the PreviewMouseDown) tunnel down from the root to the element that was clicked, and then your bubbling events (the MouseDown) bubble from that element back up to the root.

    Tunnelling and bubbling will stop whenever a control catch  that event and marks it as handled. In this case your Button will catch the left mouse down.

    Reason you don't see the Handled events is because by default you cannot catch a handled event in xaml, only in code by using element.AddHandler(RoutedEvent, Delegate, bool) with the bool set to true.

    Friday, January 19, 2007 5:05 PM