zoukankan      html  css  js  c++  java
  • WPF 附件路由事件

    public class Person

        {

            public static readonly RoutedEvent NameChangedEvent = EventManager.RegisterRoutedEvent("NameChanged", RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(Person));

            //为界面添加路由侦听

            public static void AddNameChangedHandle(DependencyObject d,RoutedEventHandler h)

            {

                UIElement e = d as UIElement;

                if(null!=e)

                {

                    e.AddHandler(NameChangedEvent, h);

                }

            }

            //移除侦听

            public static void RemoveNameChangedHandle(DependencyObject d,RoutedEventHandler h)

            {

                UIElement e = d as UIElement;

                if(null!=e)

                {

                    e.RemoveHandler(NameChangedEvent,h);

                }

            }

            public int Id { get; set; }

            public string Name { get; set; }

        }

    <Window x:Class="WpfApplication1.Window27"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            Title="Window27" Height="272" Width="349">

        <Grid x:Name="gd_main">

            <Button Content="Button"  x:Name="button1" Width="75" Height="75" Margin="10" Click="button1_Click" />

        </Grid>

    </Window>

     public partial class Window27 : Window

        {

            public Window27()

            {

                InitializeComponent();

                //为外层Grid添加路由事件

                Person.AddNameChangedHandle(this.gd_main, new RoutedEventHandler(PersonNameChanged));

            }

            private void PersonNameChanged(object obj, RoutedEventArgs e)

            {

                MessageBox.Show((e.OriginalSource as Person).Name);

            }

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                Person persion = new Person();

                persion.Id = 0;

                persion.Name = "Darren";

                //准备事件消息并发送路由事件

                RoutedEventArgs arg = new RoutedEventArgs(Person.NameChangedEvent, persion);

                this.button1.RaiseEvent(arg);

            }

        }

  • 相关阅读:
    248.Strobogrammatic Number III
    git Changes not staged for commit,部分修改文件不能确认
    python 文件运行时隐藏命令窗口,图形化界面隐藏cmd窗口
    python 处理配置文件
    odoo 内嵌报表的方式,iframe
    odoo 多个数据库http请求指定数据库
    jasperreportserver 免登录访问,匿名访问报表
    python 图形化界面点击按钮卡死
    python 字符反转
    pentaho 相关介绍
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14856153.html
Copyright © 2011-2022 走看看