在XAML代码中为节点树安装事件监听器
2015-01-11 20:26
232 查看
通过以下的演示样例代码,能够发现,我们能为随意的节点指定要监听的路由事件,而这个路由事件本身和这个元素可能根本就没有关系。
当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了。
相关文章推荐
- 在XAML代码中为节点树安装事件监听器
- XAML概要--事件和后台代码
- jquery.jstree 增加节点的双击事件代码
- XAML概要--事件和后台代码
- elasticsearch的单节点和分布式的安装及其操作(使用命令和使用代码)
- SQLServer\framework启动报异常:Module的类型初始值设定项引发异常 [转]QQ空间、新浪微博、腾讯微博等一键分享API链接代码 window下 mongodb快速安装 数据表查询所有子/父节点 C#学习笔记1
- jquery.jstree 增加节点的双击事件代码
- 动态节点的生成注意事项及仿百度搜索事件代码
- 自定OnClickListener注解,减少点击事件监听器的代码
- javascript 传统事件模型构造的事件监听器实现代码
- 跨浏览的DOM节点事件监听器
- 手动触发dom节点事件代码
- (碎片代码)Button事件添加监听器
- 跨浏览的DOM节点事件监听器
- jquery 获取绑定在某个节点上的 事件执行代码
- XAML概要--事件和后台代码
- WPF基础之XAML----(XAML 根元素和 xmlns,事件和 XAML 代码隐藏)
- js---如何通过js/jQuery代码出发节点的click事件
- CLR事件与路由事件在XAML代码中应用时的区别