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

      public class ReportTimeEventArgs:RoutedEventArgs

        {

            public ReportTimeEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }

            public DateTime ClickTime { get; set; }

        }

        public class TimeButton : Button

        {

            //声明和注册路由事件

            public static readonly RoutedEvent ReportTimeEvent = EventManager.RegisterRoutedEvent("ReportTime", RoutingStrategy.Tunnel,typeof(EventHandler<ReportTimeEventArgs>),typeof(TimeButton));

            //CLR事件包装器

            public event RoutedEventHandler ReportTime

            {

                add { this.AddHandler(ReportTimeEvent, value); }

                remove { this.RemoveHandler(ReportTimeEvent, value); }

            }

            //激发路由事件,借用Click事件的激活方法

            protected override void OnClick()

            {

                base.OnClick();//保证Button的原有功可以正常使用、Click事件能被激发。

                ReportTimeEventArgs args = new ReportTimeEventArgs(ReportTimeEvent, this);

                args.ClickTime = DateTime.Now;

                this.RaiseEvent(args);

            }

        }

    xml---------------- 代码--------------------------------

    <Window x:Class="WpfApplication1.Window25"

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

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

            xmlns:local ="clr-namespace:WpfApplication1.Model"

            Title="Window25" Height="300" Width="300" x:Name="Window225" local:TimeButton.ReportTime="ReportTimeHandle">

        <Grid x:Name="gd_1" Margin="10" Background="AliceBlue" local:TimeButton.ReportTime="ReportTimeHandle">

            <Grid x:Name="gd_2" Margin="10" Background="AntiqueWhite" local:TimeButton.ReportTime="ReportTimeHandle">

                <Grid x:Name="gd_3" Margin="10" Background="Aqua" local:TimeButton.ReportTime="ReportTimeHandle">

                    <StackPanel Margin="10" Background="Aquamarine" x:Name="sp_1" local:TimeButton.ReportTime="ReportTimeHandle">

                        <ListBox x:Name="lb_view" MinHeight="30" MaxHeight="150"></ListBox>

                        <local:TimeButton x:Name="tb_main" local:TimeButton.ReportTime="ReportTimeHandle" Content="Test" Width="50"></local:TimeButton>

                    </StackPanel>

                </Grid>

            </Grid>

        </Grid>

    </Window>

  • 相关阅读:
    javascript 中数字计算精度缺失问题
    javascript闭包
    MySQL数据库的创建
    原生项目使用 sass
    git工具命令
    如何将你的node服务放到线上服务器
    Cookie、Session、Token 的区别
    东北师大-构建之法-2020秋最终成绩(并非期末成绩)
    20201220-东北师范大学-助教-周总结-第14次
    东北师范大学-构建之法-20201207作业成绩
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14856127.html
Copyright © 2011-2022 走看看