zoukankan      html  css  js  c++  java
  • WPF Attached event

    Code behind:

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Shapes;

    using Microsoft.Win32;

    using System.Globalization;

     

    namespace WpfApplication54

    {

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

    public MainWindow()

    {

    InitializeComponent();

    this.AddHandler(AttachedEventTest.NeedsCleaningEvent, new RoutedEventHandler((object o, RoutedEventArgs e) => { MessageBox.Show("from window"); }));

    // use true to handle this event too in MainWindow level though e.handled is marked as true in grid_NeedsCleaning method.

    //this.AddHandler(AttachedEventTest.NeedsCleaningEvent, new RoutedEventHandler((object o, RoutedEventArgs e) => { MessageBox.Show("from window"); }), true);

    }

     

    private void button1_Click(object sender, RoutedEventArgs e)

    {

    this.grid.RaiseEvent(new RoutedEventArgs(AttachedEventTest.NeedsCleaningEvent));

    }

     

    private void grid_NeedsCleaning(object sender, RoutedEventArgs e)

    {

    MessageBox.Show("from grid");

    //e.Handled = true;

    }

    }

     

    // for this case, note that, if NeedsCleaningEvent's RoutingStrategy is Bubble, the order of windows pop up is: from grid -> from window(frist catched by grid, then window);

    // if NeedsCleaningEvent's RoutingStrategy is Tunnel, the order of windows pop up is: from window -> from grid;

    public class AttachedEventTest

    {

    // below is the pattern of AttachedEvent, it uses the same method RegisterRoutedEvent as non-attached event.

    // the Add*Handler and Remove*Handler are must, xaml will use it when setting attached event for an element in xaml.

    public static readonly RoutedEvent NeedsCleaningEvent = EventManager.RegisterRoutedEvent("NeedsCleaning", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AttachedEventTest));

     

    public static void AddNeedsCleaningHandler(DependencyObject d, RoutedEventHandler handler)

    {

    UIElement uie = d as UIElement;

    if (uie != null)

    {

    uie.AddHandler(AttachedEventTest.NeedsCleaningEvent, handler);

    }

    }

    public static void RemoveNeedsCleaningHandler(DependencyObject d, RoutedEventHandler handler)

    {

    UIElement uie = d as UIElement;

    if (uie != null)

    {

    uie.RemoveHandler(AttachedEventTest.NeedsCleaningEvent, handler);

    }

    }

    }

    }

     

    Xaml:

     

    <Window x:Class="WpfApplication54.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"

    xmlns:local="clr-namespace:WpfApplication54">

    <Grid x:Name="grid" local:AttachedEventTest.NeedsCleaning="grid_NeedsCleaning" >

    <Button Content="Button1" Click="button1_Click" Height="23" HorizontalAlignment="Left" Margin="174,118,0,0" Name="button1" VerticalAlignment="Top" Width="75" />

    </Grid>

    </Window>

     

  • 相关阅读:
    入职小白随笔之Android四大组件——内容提供器详解(Content Provider)
    入职小白随笔之Android四大组件——广播详解(broadcast)
    入职小白随笔之Android四大组件——活动详解(activity)
    入职小白随笔之高通项目编译流程
    Java基础学习-Random类和Java数组
    Java基础学习-流程控制语句
    Java基础学习-三元运算符和键盘录入的基本步骤和使用
    Java基础学习-Eclipse综述和运算符的使用
    Java基础学习-类型转换之隐式转换
    SQLite----Android Studio3.6.3 当前最新版本数据库查找与导出方法
  • 原文地址:https://www.cnblogs.com/bear831204/p/2111908.html
Copyright © 2011-2022 走看看