问题描述
在下面的code,我看到发生的,但我没有看到发生相应的事件冒泡隧道事件。为什么会这样呢?
谢谢,戴夫
<窗口x:类=TestRoutedEvents.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1高度=300宽度=300
previewMouseUp =Window_ previewMouseUp
的MouseUp =Window_MouseUp>
<网格背景=布朗保证金=30
previewMouseUp =Grid_ previewMouseUp
的MouseUp =Grid_MouseUp>
< TextBlock的文本=preSS我的HorizontalAlignment =中心VerticalAlignment =中心
背景=浅灰色
填充=3
previewMouseUp =TextBlock_ previewMouseUp
的MouseUp =TextBlock_MouseUp/>
< /网格>
< /窗>
命名空间TestRoutedEvents
{
///<总结>
///为Window1.xaml交互逻辑
///< /总结>
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent();
}
私人无效TextBlock_ previewMouseUp(对象发件人,MouseButtonEventArgs E)
{
的MessageBox.show(TextBlock_ previewMouseUp);
}
私人无效Grid_ previewMouseUp(对象发件人,MouseButtonEventArgs E)
{
的MessageBox.show(Grid_ previewMouseUp);
}
私人无效Window_ previewMouseUp(对象发件人,MouseButtonEventArgs E)
{
的MessageBox.show(Window_ previewMouseUp);
}
私人无效TextBlock_MouseUp(对象发件人,MouseButtonEventArgs E)
{
的MessageBox.show(TextBlock_MouseUp);
}
私人无效Grid_MouseUp(对象发件人,MouseButtonEventArgs E)
{
的MessageBox.show(Grid_MouseUp);
}
私人无效Window_MouseUp(对象发件人,MouseButtonEventArgs E)
{
的MessageBox.show(Window_MouseUp);
}
}
}
其实事件被提高,但你是不是能够得到的。其原因在于,messagbox把焦点从窗口弹出时。因此,在路由事件链中的UI元素将不会收到路由事件的任何其他。
您可以控制你的窗口,并添加事件的详细信息到该确认一下吧。例如添加列表框在XAML并将其命名为像列表框,然后用这个code。在您的每一个处理程序
listBox.Items.Add(发件人+\ N+ e.RoutedEvent.Name +\ N+ e.RoutingStrategy);
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");
}
}
}
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);
这篇关于冒泡事件未发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!