Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't button click event "bubble up visual tree" to StackPanel as MSDN article states?
    primarykey
    data
    text
    <p>In the MSDN article <a href="http://msdn.microsoft.com/en-us/magazine/cc785480.aspx" rel="noreferrer">Understanding Routed Events and Commands In WPF</a>, it states</p> <blockquote> <p><em>an event will bubble (propagate) up the visual tree from the source element until either it has been handled or it reaches the root element.</em></p> </blockquote> <p>However, in this example, when you click the button, <strong>it doesn't "bubble up the visual tree" to get handled by the parent StackPanel event</strong>, i.e. clicking on the button fires no event.</p> <p>Why not? <strong>What do they mean then by "bubbling up"</strong> if not this?</p> <p><strong>XAML:</strong></p> <pre><code>&lt;Window x:Class="TestClickEvents456.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;StackPanel x:Name="TheStackPanel" Background="Yellow" MouseDown="TheStackPanel_MouseDown"&gt; &lt;Button x:Name="TheButton" Margin="10" Content="Click This"/&gt; &lt;TextBlock x:Name="TheMessage" Text="Click the button or the yellow area"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p><strong>code-behind:</strong></p> <pre><code>using System.Windows; using System.Windows.Input; namespace TestClickEvents456 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void TheStackPanel_MouseDown(object sender, MouseButtonEventArgs e) { TheMessage.Text = "StackPanel was clicked."; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload