Microsoft 官方概述:
附加事件可用于在非元素类中定义新的 路由事件 ,并在树中的任何元素上引发该事件。 为此,必须将附加事件注册为路由事件,并提供支持附加事件功能的特定 支持代码 。 由于附加事件注册为路由事件,因此在元素树中引发时,它们会传播到元素树中。
简单来说就是,可以进行附加操作的事件,必须为路由事件(
RoutedEvent
)。
在 XAML 语法中,附加事件由其
事件名称
和所有者
类型指定,格式为。 由于事件名称使用其所有者类型的名称进行限定,因此语法允许事件附加到任何可以实例化的元素。 此语法也适用于附加到事件路由中任意元素的常规路由事件的处理程序。
.
这里简略的借用文档中的说明,详细学习请以文档为准:附加事件概述 (WPF .NET)
using System.Windows;
using System.Windows.Controls;namespace assembly
{/// /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。////// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根/// 元素中:////// xmlns:MyNamespace="clr-namespace:assembly"///////// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根/// 元素中:////// xmlns:MyNamespace="clr-namespace:assembly;assembly=assembly"////// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,/// 并重新生成以避免编译错误:////// 在解决方案资源管理器中右击目标项目,然后依次单击/// “添加引用”->“项目”->[浏览查找并选择此项目]///////// 步骤 2)/// 继续操作并在 XAML 文件中使用控件。////// ////// public class MessagePopup : UserControl{public static readonly DependencyProperty BusinessTypeProperty =DependencyProperty.Register(nameof(BusinessType), typeof(string), typeof(MessagePopup), new PropertyMetadata(string.Empty));/// /// 业务类型/// public string BusinessType{get { return (string)GetValue(BusinessTypeProperty); }set { SetValue(BusinessTypeProperty, value); }}public static readonly DependencyProperty SecondsProperty =DependencyProperty.Register(nameof(Seconds), typeof(string), typeof(MessagePopup), new PropertyMetadata(string.Empty));/// /// 秒数/// public string Seconds{get { return (string)GetValue(SecondsProperty); }set { SetValue(SecondsProperty, value); }}static MessagePopup(){DefaultStyleKeyProperty.OverrideMetadata(typeof(MessagePopup), new FrameworkPropertyMetadata(typeof(MessagePopup)));}}
}
下面的样式使用 ControlTemplate 自定义的控件样式,其中的 Button 按钮是不能进行事件绑定的,当然命令绑定也是不行的。
使用该自定义控件时,为其注册
Button.Click
事件,属于原生路由事件。对应其用方法可以得出,该事件触发的是 Button 元素的 Click 事件。
简单来说就是,只有自定义控件中的 Button 元素才能触发当前的 Click 事件。(附加事件用法)
StaticResource popup}"Visibility="Visible" />
元素树中声明了两个 Buuton 这里暂且通过 Button 的 Tag 值来区分,不仅限于 Tag。
private void popup_Click(object sender, RoutedEventArgs e)
{string tag = (string)((FrameworkElement)e.OriginalSource).Tag;if (tag == "0"){MessageBox.Show("是关闭按钮");//CloseTimer();}else if (tag == "1"){MessageBox.Show("是跳转按钮");}
}