在XAML代码中为节点树安装事件监听器
2023-09-14 09:08:15 时间
通过以下的演示样例代码,能够发现,我们能为随意的节点指定要监听的路由事件,而这个路由事件本身和这个元素可能根本就没有关系。
当ReportTime的路由事件被触发之后,该事件会沿着树进行传递,假设到了某个节点我们已经把该事件处理了,而且无需继续往上传递,则能够通过其參数e.Handled=true来控制其无需继续传递下去了。例如以下代码所看到的:
依照上面的代码,当路由事件传递给Grid2的时候,就被标记为已经处理了,这样路由事件就不会再传递给Grid1了。
<Window x:Class="Demo002.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Demo002" Title="MainWindow" Height="350" Width="525"> <Grid x:Name="Grid1" local:TimeButton.ReportTime="ReportTimeHandler"> <Grid x:Name="Grid2" local:TimeButton.ReportTime="ReportTimeHandler"> <Grid x:Name="Grid3" local:TimeButton.ReportTime="ReportTimeHandler"> <StackPanel x:Name="StackPanel1" local:TimeButton.ReportTime="ReportTimeHandler"> <ListBox x:Name="ListBox1" /> <local:TimeButton x:Name="TimeButton1" Width="80" Height="80" Content="Report Time" local:TimeButton.ReportTime="ReportTimeHandler"/> </StackPanel> </Grid> </Grid> </Grid> </Window>
当ReportTime的路由事件被触发之后,该事件会沿着树进行传递,假设到了某个节点我们已经把该事件处理了,而且无需继续往上传递,则能够通过其參数e.Handled=true来控制其无需继续传递下去了。例如以下代码所看到的:
private void ReportTimeHandler(object sender, ReportTimeEventArgs e) { FrameworkElement element = sender as FrameworkElement; string content = string.Format("{0}到达{1}", e.ClickTime.ToLongTimeString(), element.Name); this.ListBox1.Items.Add(content); if (element == this.Grid2) { e.Handled = true; } }
依照上面的代码,当路由事件传递给Grid2的时候,就被标记为已经处理了,这样路由事件就不会再传递给Grid1了。
相关文章
- 程序员的量化交易之路(5)--Esper之Map事件(4)
- 关于取消DOM 0级事件和2级事件时的小坑
- C#委托和事件
- jQuery 事件的命名空间的含义
- SAP UI5对mobile touch事件的支持
- atitit ui引擎之道 游戏引擎之道.docx 1. 概念2 1.1. (cocos,createjs,dom)2 2. 游戏引擎的构成(图形引擎(渲染系统),控件部件系统,事件系统 ,布局
- Android编程输入事件流程详解
- Android Input输入系统之一:KeyEvent事件监听
- Qt5键盘事件
- brython 文档之鼠标事件
- Vue2.js:v-click-outside自定义指令和vue-click-outside监听鼠标点击元素外部区域事件
- wpf程序启动事件
- WPF 附件路由事件