开发者

Bubbling Events not Occurring

In the code below, I am seeing the tunneling events occurring but am not seeing the corresponding bubbling events occurring. Why might this be?

Thanks, Dave

<Window x:Class="TestRoutedEvents.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"
   PreviewMouseUp="Window_PreviewMouseUp"
   MouseUp="Window_MouseUp">

   <Grid Background="Brown" Margin="30"
         PreviewMouseUp="Grid_PreviewMouseUp"
         MouseUp="Grid_MouseUp">
      <TextBlock Text="Press me" HorizontalAlignment="Center" VerticalAlignment="Center"
         Background="LightGray"
         Padding="3"
         PreviewMouseUp="TextBlock_PreviewMouseUp"
         MouseUp="TextBlock_MouseUp"/>
   </Grid>
</Window>


namespace TestRoutedEvents
{
   /// <summary>
   /// Interaction logic for Window1.xaml
   /// </summary>
   public partial class Window1 : Window
   {
      public Window1()
      {
         InitializeComponent();
      }





      private void TextBlock_PreviewMouseUp(object sender, MouseButtonEventArgs e)
      {
         MessageBox.Show("TextBlock_PreviewMouseUp");
      }

      private void Grid_PreviewMouseUp(object sender, MouseButtonEventArgs e)
      {
         MessageBox.Show("Grid_PreviewMouseUp");
      }

      private void Window_PreviewMouseUp(object sender, MouseButtonEventArgs e)
      {
         MessageBox.Show("Window_PreviewMouseUp");
      }





      private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e)
      {
         MessageBox.Show("TextBlock_MouseUp");
      }

      private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
      {
         MessageBox.Show("Grid_MouseUp");
      }

      private void Window_MouseUp(object sender, MouseButtonEventArgs e)
      {
         MessageBox.Show("Window_MouseUp");
      }
   }
}
开发者_JAVA技巧


Actually event is raising but you are not able to get that. The reason is that the messagbox takes the focus from the window when it pops up. Thus, the UI elements in the routed event chain won't receive the routed event any more.

you can have a control to your window and add event details into that to confirm about it. e.g. Add ListBox in xaml and name it like listBox and then use this code in each of your handler

listBox.Items.Add(sender+"\n"+e.RoutedEvent.Name+"\n"+e.RoutingStrategy);


This is a quote from MSDN:

TextBox has built-in handling for the bubbling MouseUp and events. Consequently, custom event handlers that listen for MouseUp or MouseDown events from a TextBox will never be called. If you need to respond to these events, listen for the tunneling PreviewMouseUp and PreviewMouseDown events instead.

The article is about TextBox not TextBlock, but I tested this on other UIElement-derived controls, and they all seemed to behave the same way. I'm assuming this is at the base class level somewhere in the hierarchy.

How to: Handle MouseUp and MouseDown Events for a TextBox


I don't know what you are trying to do with this, but perhaps this post will help. It confirms what Anurag is saying, regarding the messagebox taking the focus from your window.

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f4d609d4-ba2c-478e-aa53-9ee557ea5165

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜