一文帶你了解WPF中的附加事件
什么是附加事件
Microsoft 官方概述:
附加事件可用于在非元素類中定義新的 路由事件 ,并在樹中的任何元素上引發(fā)該事件。 為此,必須將附加事件注冊(cè)為路由事件,并提供支持附加事件功能的特定 支持代碼 。 由于附加事件注冊(cè)為路由事件,因此在元素樹中引發(fā)時(shí),它們會(huì)傳播到元素樹中。
簡單來說就是,可以進(jìn)行附加操作的事件,必須為路由事件(RoutedEvent)。
附加事件用法
在 XAML 語法中,附加事件由其 事件名稱和所有者 類型指定,格式為 <owner type>.<event name>。 由于事件名稱使用其所有者類型的名稱進(jìn)行限定,因此語法允許事件附加到任何可以實(shí)例化的元素。 此語法也適用于附加到事件路由中任意元素的常規(guī)路由事件的處理程序。
Microsoft 官方文檔
這里簡略的借用文檔中的說明,詳細(xì)學(xué)習(xí)請(qǐng)以文檔為準(zhǔn):附加事件概述 (WPF .NET)
附加事件案例
定義自定義控件
using System.Windows;
using System.Windows.Controls;
namespace assembly
{
/// <summary>
/// 按照步驟 1a 或 1b 操作,然后執(zhí)行步驟 2 以在 XAML 文件中使用此自定義控件。
///
/// 步驟 1a) 在當(dāng)前項(xiàng)目中存在的 XAML 文件中使用該自定義控件。
/// 將此 XmlNamespace 特性添加到要使用該特性的標(biāo)記文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:assembly"
///
///
/// 步驟 1b) 在其他項(xiàng)目中存在的 XAML 文件中使用該自定義控件。
/// 將此 XmlNamespace 特性添加到要使用該特性的標(biāo)記文件的根
/// 元素中:
///
/// xmlns:MyNamespace="clr-namespace:assembly;assembly=assembly"
///
/// 您還需要添加一個(gè)從 XAML 文件所在的項(xiàng)目到此項(xiàng)目的項(xiàng)目引用,
/// 并重新生成以避免編譯錯(cuò)誤:
///
/// 在解決方案資源管理器中右擊目標(biāo)項(xiàng)目,然后依次單擊
/// “添加引用”->“項(xiàng)目”->[瀏覽查找并選擇此項(xiàng)目]
///
///
/// 步驟 2)
/// 繼續(xù)操作并在 XAML 文件中使用控件。
///
/// <MyNamespace:MessagePopup/>
///
/// </summary>
public class MessagePopup : UserControl
{
public static readonly DependencyProperty BusinessTypeProperty =
DependencyProperty.Register(nameof(BusinessType), typeof(string), typeof(MessagePopup), new PropertyMetadata(string.Empty));
/// <summary>
/// 業(yè)務(wù)類型
/// </summary>
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));
/// <summary>
/// 秒數(shù)
/// </summary>
public string Seconds
{
get { return (string)GetValue(SecondsProperty); }
set { SetValue(SecondsProperty, value); }
}
static MessagePopup()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MessagePopup), new FrameworkPropertyMetadata(typeof(MessagePopup)));
}
}
}
自定義控件樣式
下面的樣式使用 ControlTemplate 自定義的控件樣式,其中的 Button 按鈕是不能進(jìn)行事件綁定的,當(dāng)然命令綁定也是不行的。
<Application
x:Class="assembly.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:assembly"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<!--#region-->
<Style x:Key="popup" TargetType="{x:Type local:MessagePopup}">
<Setter Property="Height" Value="436" />
<Setter Property="Width" Value="840" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MessagePopup}">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="pack://application:,,,/images/bg-Tips.png" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="0.7*" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="55,0">
<Grid.RowDefinitions>
<RowDefinition Height="0.5*" />
<RowDefinition />
</Grid.RowDefinitions>
<Button
Width="100"
Height="40"
Margin="0,20,0,0"
HorizontalAlignment="Right"
Tag="0">
<Button.Template>
<ControlTemplate>
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<Image Source="pack://application:,,,/images/ico-close.png" />
<TextBlock
Margin="5,0,0,0"
VerticalAlignment="Center"
FontSize="28"
FontWeight="Black"
Foreground="#7582CC"
Text="關(guān)閉" />
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
<StackPanel
Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock
FontSize="42"
FontWeight="Black"
Text="" />
<TextBlock
FontSize="42"
FontWeight="Black"
Foreground="#F95A00"
Text="{TemplateBinding BusinessType}" />
<TextBlock
FontSize="42"
FontWeight="Black"
Text="" />
</StackPanel>
</Grid>
<Line
Grid.Row="1"
Margin="80,0"
Stroke="#8DB2BD"
StrokeDashArray="2,2"
StrokeThickness="4"
X1="0"
X2="650" />
<Grid Grid.Row="2" Margin="55,0">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="75,0">
<StackPanel
Margin="0,30,0,0"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock
VerticalAlignment="Center"
FontSize="32"
Foreground="#002C86"
Text="" />
<Image
Height="40"
Margin="20,0"
Source="pack://application:,,,/images/ico-arrow.png" />
<Button
Width="180"
Height="50"
Tag="1">
<Button.Template>
<ControlTemplate>
<Border Background="#DDE2FF" CornerRadius="5">
<StackPanel
Height="40"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<Image Source="pack://application:,,,/images/ico-filer.png" />
<TextBlock
Margin="5,0,0,0"
VerticalAlignment="Center"
FontSize="28"
FontWeight="Black"
Foreground="#7582CC"
Text="跳轉(zhuǎn)頁面" />
</StackPanel>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<StackPanel
Margin="0,20,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock
FontSize="38"
Foreground="#FFD873"
Text="{TemplateBinding Seconds}" />
<TextBlock
FontSize="38"
Foreground="#FFD873"
Text="" />
<TextBlock
Margin="10,0,0,0"
VerticalAlignment="Bottom"
FontSize="32"
Foreground="#FFFFFF"
Text="" />
</StackPanel>
</Grid>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--#endregion-->
</ResourceDictionary>
</Application.Resources>
</Application>效果

注冊(cè)使用附加事件
使用該自定義控件時(shí),為其注冊(cè) Button.Click 事件,屬于原生路由事件。對(duì)應(yīng)其用方法可以得出,該事件觸發(fā)的是 Button 元素的 Click 事件。
簡單來說就是,只有自定義控件中的 Button 元素才能觸發(fā)當(dāng)前的 Click 事件。(附加事件用法)
<Window
x:Class="assembly.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:comm="clr-namespace:assembly"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="900"
Height="550"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Grid>
<comm:MessagePopup
x:Name="popup"
Width="840"
Height="436"
Button.Click="popup_Click"
Style="{StaticResource popup}"
Visibility="Visible" />
</Grid>
</Window>元素樹中聲明了兩個(gè) Buuton 這里暫且通過 Button 的 Tag 值來區(qū)分,不僅限于 Tag。
private void popup_Click(object sender, RoutedEventArgs e)
{
string tag = (string)((FrameworkElement)e.OriginalSource).Tag;
if (tag == "0")
{
MessageBox.Show("是關(guān)閉按鈕");
//CloseTimer();
}
else if (tag == "1")
{
MessageBox.Show("是跳轉(zhuǎn)按鈕");
}
}
效果

到此這篇關(guān)于一文帶你了解WPF中的附加事件的文章就介紹到這了,更多相關(guān)WPF附加事件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#對(duì)稱加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例
這篇文章主要介紹了C#對(duì)稱加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例,每次解密時(shí)從密文中截取前16位,這就是實(shí)現(xiàn)隨機(jī)的奧秘,本文同時(shí)給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-07-07
C#結(jié)束Excel進(jìn)程的步驟教學(xué)
在本篇文章里小編給大家分享了關(guān)于C#結(jié)束Excel進(jìn)程的步驟教學(xué)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-01-01
C#代碼實(shí)現(xiàn)短信驗(yàn)證碼接口示例
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)短信驗(yàn)證碼接口示例代碼,感興趣的小伙伴們可以參考一下2016-08-08
C# 使用PictureBox實(shí)現(xiàn)圖片按鈕控件的示例步驟
這篇文章主要介紹了C# 使用PictureBox實(shí)現(xiàn)圖片按鈕控件的示例步驟,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02
WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解
這篇文章主要介紹了WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C#隨機(jī)設(shè)置900-1100毫秒延遲的方法
這篇文章主要介紹了C#隨機(jī)設(shè)置900-1100毫秒延遲的方法,涉及C#中Thread.Sleep方法的使用技巧,需要的朋友可以參考下2015-04-04

