Page 111 附加事件
2011-11-08 11:32
197 查看
假设StackPanel包装了一些按钮,并且希望一个事件中处理程序中所有这些按钮的单击事件。粗略的方法是将每个按钮的Click事件关联到同一个事件处理程序。
但是Click支持事件冒泡,从而提供了一个更好的选择。可以通过更高层次元素的Click事件(如StackPanel面板)来处理所有按钮的单击事件。
问题是XAML解析器会降上述代码解释成一个错误,因为StackPanel没有Click事件;
解决方法为“对象.事件名”的形式使用不同的关联事件语法。
Click事件实际是ButtonBase类中定义的,并且Button类继承了该事件。如果为ButtonBase.Click事件关联事件处理程序,那么当任何继承自ButtonBase(包括Button类、RadioButton类以及CheckBox类)被单击时,都会调用该事件处理程序。如果为Button.Click事件关联事件处理程序,那么事件处理程序就只能被Button对象使用。
但是Click支持事件冒泡,从而提供了一个更好的选择。可以通过更高层次元素的Click事件(如StackPanel面板)来处理所有按钮的单击事件。
<Window x:Class="Page111.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel Orientation="Vertical" Margin="30" Click="DoSomething"> <Button Margin="10" Width="75" Height="25">Command 1</Button> <Button Margin="10" Width="75" Height="25">Command 2</Button> <Button Margin="10" Width="75" Height="25">Command 3</Button> </StackPanel> </Grid> </Window>
问题是XAML解析器会降上述代码解释成一个错误,因为StackPanel没有Click事件;
解决方法为“对象.事件名”的形式使用不同的关联事件语法。
<Window x:Class="Page111.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel Orientation="Vertical" Margin="30" Button.Click="DoSomething"> <Button Margin="10" Width="75" Height="25">Command 1</Button> <Button Margin="10" Width="75" Height="25">Command 2</Button> <Button Margin="10" Width="75" Height="25">Command 3</Button> </StackPanel> </Grid> </Window>
Click事件实际是ButtonBase类中定义的,并且Button类继承了该事件。如果为ButtonBase.Click事件关联事件处理程序,那么当任何继承自ButtonBase(包括Button类、RadioButton类以及CheckBox类)被单击时,都会调用该事件处理程序。如果为Button.Click事件关联事件处理程序,那么事件处理程序就只能被Button对象使用。
相关文章推荐
- WPF快速入门1-XAML(window、Page、Application、属性、附加属性、特殊字符、保留空白、事件)
- c# tabcontrol 点击不同的tabpage 触发不同的事件
- 深入浅出附加事件(转)
- Page激活事件顺序
- mobile.changePage切换页面后的事件处理
- jquery page 事件加载顺序 生命周期
- BasePage 撰写 PageCommand 事件
- ASP.NET_Page事件的执行顺序
- WPF(二)路由事件3.附加事件
- asp.net Page_Load事件加载两次
- System.Web.UI.Page事件执行顺序
- 2012-10-24 11gR2 "ADMINISTRATOR'S GUIDE" page 111 - 218
- ASP.NET的Page_Load事件
- 母板页中的事件Page_Load居然不执行
- 运行page页面时的事件执行顺序
- 关于Page_Load事件发生情况
- 实现全站统一的Page_PreInit()等事件
- 一段常用的Page_Load事件
- .net Page 执行事件顺序
- Google Page Creator之404事件