WPF Demo18 Routed Events

Source: Internet
Author: User

Using System.windows;namespace routed event 2{public    class Student    {        ////declares and defines routed events        //public static readonly RoutedEvent namechangedevent =        //    eventmanager.registerroutedevent ("namechanged",        //    Routingstrategy.bubble,        //    typeof (Routedeventhandler),         //    typeof (Student));        private int id;        public int ID        {            get {return Id;}            set {id = value;}        }        private string name;        public string name        {            get {return Name;}            set {name = value;}}}}    
<Windowx:class= "routed event 2." MainWindow "xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= " the"Width= "525">    <Gridx:name= "Testgrid">        <Buttonx:name= "Btntest"Content= "OK"Width= "a"Height= " the"FontSize= "+"Click= "btnTest_Click"/>    </Grid></Window>
Using System.windows;namespace routed event 2{///<summary>//MainWindow.xaml interactive logic///</summary> Publi            C Partial class Mainwindow:window {//Declare and define routed events public static readonly RoutedEvent namechangedevent = Eventmanager.registerroutedevent ("namechanged", Routingstrategy.bubble, typeof (RoutedEvent        Handler), typeof (MainWindow));            Public MainWindow () {InitializeComponent ();        Adds a routed event listener This.testGrid.AddHandler (namechangedevent, New Routedeventhandler (Studentnamechangeevent)) to the grid; } private void btnTest_Click (object sender, RoutedEventArgs e) {Student stu = new Student (            {Id = 1, Name = "name001"}; Stu.            Name = "name007";            Prepares the event message and sends the routed event RoutedEventArgs arg = new RoutedEventArgs (namechangedevent, Stu); RaiseEvent for triggering routed events this. btntest.raiseevent (ARG);  } public void Studentnamechangeevent (object sender, RoutedEventArgs e) {MessageBox.Show ("id==" + (E.originalsource as Student). Id.tostring () + "\ n" + "name==" + (E.originalsource as Student).        Name.tostring ()); }    }}

Example two:

<Windowx:class= "routed event 3." MainWindow "xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"Title= "MainWindow"Height= " the"Width= "525">    <Gridx:name= "Testgrid">        <Buttonx:name= "Btntest"Content= "OK"Width= "a"Height= " the"FontSize= "+"Click= "btnTest_Click"/>    </Grid></Window>
Using System.windows;namespace routed event 3{public class Student {//Declare and define routed events public static readonly Routed Event namechangedevent = eventmanager.registerroutedevent ("NameChange", Routingstrategy.bubble,typeof (RoutedEve        Nthandler), typeof (Student));            Add a route listener public static void Addnamechangedhandler (DependencyObject d,routedeventhandler h) for interface elements {            UIElement e = d as UIElement;        if (E! = null) E.addhandler (student.namechangedevent, h);            }//Remove listen public static void Removenamechangedhandler (DependencyObject D, Routedeventhandler h) {            UIElement e = d as UIElement;        if (E! = null) E.removehandler (student.namechangedevent, h);        } private int id;            public int ID {get {return Id;}        set {id = value;}        } private string name;            public string Name {get {return Name;} set {name = value;}}}}using System.windows;namespace routed event 3{//<summary>///MainWindow.xaml interactive logic///</summ Ary> public partial class Mainwindow:window {public MainWindow () {Initializecomponen            T ();        Add the routed event listener Student.addnamechangedhandler (this.testgrid,new Routedeventhandler (namechangedevent)) for the outer grid; } public void Namechangedevent (Object Sender,routedeventargs e) {MessageBox.Show ("id==" + (E. OriginalSource as Student). Id.tostring () + "\ n" + "name==" + (E.originalsource as Student).        Name.tostring ());            private void btnTest_Click (object sender, RoutedEventArgs e) {Student stu = new Student ()            {Id = 1, Name = "001"}; Stu.            Name = "002";            Prepares the event message and sends the routed event RoutedEventArgs arg = new RoutedEventArgs (student.namechangedevent, Stu); This.Btntest.raiseevent (ARG); }    }}

WPF Demo18 Routed Events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.