Two methods of WPF data binding, interface refresh-----inotifypropertychanged

Source: Internet
Author: User

. Netformwork4.0 and the following versions-------INotifyPropertyChanged

Namespaces: System.ComponentModel

Background code

Public partial class Dvrwnd:usercontrol {public Dvrwnd () {InitializeComponent (); private void Initinfo () {for (int i = 0; i <; i++) {Dvrinfo D                Vrinfo = new Dvrinfo ();                Dvrinfo.strdvrname = "Encoder" + i.tostring ();                Dvrinfo.strdvrip = "10.10.13.10" + i.tostring ();                Dvrinfo.strdvrport = "506" + i.tostring ();                Dvrinfo.strdvrtype = "Type" + i.tostring ();                Dvrinfo.strdvrcode = "340201030002150000" + i.tostring ();            DVRINFOLIST.ITEMS.ADD (Dvrinfo); }} private void Usercontrol_loaded (object sender, RoutedEventArgs e) {dvrinfolist.items.            Clear ();        Initinfo ();        }//encoder information, as long as you change the properties in the class, you can use public class Dvrinfo:inotifypropertychanged {private String M_strdvrip = String.            Empty; private string m_strdvrport = String.            Empty; private string m_strdvrname = String.            Empty; private string M_strdvrtype = String.            Empty; private string M_strdvrcode = String.            Empty;                    public string Strdvrip {set {if (M_strdvrip! = value)                    {M_strdvrip = value;                } onpropertychanged ("Strdvrip");                } get {return m_strdvrip; }} public string Strdvrport {set {if                    (M_strdvrport! = value)                        {m_strdvrport = value;                    OnPropertyChanged ("Strdvrport");               } } get {return m_strdvrport; }} public string Strdvrname {set {if                    (M_strdvrname! = value)                        {m_strdvrname = value;                    OnPropertyChanged ("Strdvrname");                }} get {return m_strdvrname; }} public string Strdvrtype {set {if                    (M_strdvrtype! = value)                        {M_strdvrtype = value;                    OnPropertyChanged ("Strdvrtype");                }} get {return m_strdvrtype;             }} public string Strdvrcode {set {       if (m_strdvrcode! = value) {M_strdvrcode = value;                    OnPropertyChanged ("Strdvrcode");                }} get {return m_strdvrcode;            }} public event PropertyChangedEventHandler propertychanged;                private void OnPropertyChanged (string strpropertyinfo) {if (propertychanged! = null)                {propertychanged (this, new PropertyChangedEventArgs (Strpropertyinfo)); }            }        }

  

Front-end Code binding

<usercontrol x:class= "Testdevice.dvrwnd" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc= "http://schemas.openxmlformats.org /markup-compatibility/2006 "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "Mc:igno rable= "D" d:designheight= "d:designwidth=" background= "white" loaded= "Usercon"            trol_loaded "> <Grid> <datagrid name=" dvrinfolist "autogeneratecolumns=" False "> <DataGrid.Columns> <datagridtextcolumn header= "encoder name" isreadonly= "True" width= "+" Binding = " {BindingStrdvrname} "/>
The red Mark part is onpropertychanged ("Strdvrname");Strdvrname, not dvrinfo.strdvrname strdvrname <datagridtextcolumn header= "Encoder IP" width= "*" isreadonly= "True" Bindi ng= "{BindingStrdvrip} "/> <datagridtextcolumn header=" encoder port "width=" * "isreadonly=" True "binding=" {BindingStrdvrport} "/> <datagridtextcolumn header=" encoder type "width=" * "isreadonly=" True "binding=" {BindingStrdvrtype} "/> <datagridtextcolumn header=" encoder encoded "width=" "isreadonly=" True "binding=" {BindingStrdvrcode} "/> </DataGrid.Columns> </DataGrid> </Grid></UserControl>

  

  

. Netformwork4.5 and later-------Inotifypropertychanged,callermembername

Namespaces: System.ComponentModel

System.Runtime.CompilerServices

Background code:

        Public Cvrwnd () {InitializeComponent (); private void Initinfo () {for (int i = 0; i <; i++) {Cvrinfo C                Vrinfo = new Cvrinfo ();                Cvrinfo.strcvrname = "Disk array" + i.tostring ();                Cvrinfo.strcvrip = "10.10.13.10" + i.tostring ();                Cvrinfo.strcvrport = "506" + i.tostring ();                Cvrinfo.strcvrtype = "Type" + i.tostring ();                Cvrinfo.strcvrcode = "340201030002150000" + i.tostring ();            CVRINFOLIST.ITEMS.ADD (Cvrinfo); }} private void Usercontrol_loaded (object sender, RoutedEventArgs e) {cvrinfolist.items.            Clear ();        Initinfo ();  }//disk array information       public class Cvrinfo:inotifypropertychanged {private String M_strcvrip = String.            Empty; private string m_strcvrport = String.            Empty; private string m_strcvrname = String.            Empty; private string M_strcvrtype = String.            Empty; private string M_strcvrcode = String.            Empty; public string Strcvrip {set {Updateproperty (ref m_strcvrip,                Value);                } get {return m_strcvrip; }} public string Strcvrport {set {UPD                Ateproperty (ref m_strcvrport, value);                } get {return m_strcvrport; }} public string Strcvrname {set {UPD Ateproperty (ref m_strcvrname, value);                } get {return m_strcvrname; }} public string Strcvrtype {set {UPD                Ateproperty (ref m_strcvrtype, value);                } get {return m_strcvrtype; }} public string Strcvrcode {set {UPD                Ateproperty (ref m_strcvrcode, value);                } get {return m_strcvrcode; }} private void Updateproperty<t> (ref T Propervalue, T newvalue, [Callermembername] string PR Opertyname = "") {if (object.                Equals (Propervalue, NewValue)) {return;                } propervalue = newvalue; OnPropertyChanged (propErtyname);            } public event PropertyChangedEventHandler propertychanged; protected void OnPropertyChanged ([callermembername]string propertyname = "") {Propertychangedev                Enthandler handler = propertychanged;                if (handler! = null) {Handler (this, new PropertyChangedEventArgs (PropertyName)); }            }        }}

Front Code:

    <Grid>        <datagrid name= "cvrinfolist" autogeneratecolumns= "False" >            <DataGrid.Columns>                <datagridtextcolumn header= "disk array name" width= "*" binding= "{Binding strcvrname}"/>
Strcvrname for Cvrinfo.strcvrname <datagridtextcolumn header= "disk array IP" width= "*" binding= "{Binding Strcvrip}"/ > <datagridtextcolumn header= "disk array port" width= "*" binding= "{Binding strcvrport}"/> < Datagridtextcolumn header= "disk array type" width= "*" binding= "{Binding strcvrtype}"/> <datagridtextcolumn Header= " Disk array Encoding "width=" * "binding=" {Binding strcvrcode} "/> </DataGrid.Columns> </DataGrid> </Grid>

Both of these methods can be data binding (DataGrid, TreeView and Other controls), interface refresh, the first method is supported in. netfromwork version 4.0, the disadvantage of this method is that when the properties of the class are more, If you suddenly change the value in OnPropertyChanged (value), the interface cannot be synchronized, and it feels like a single line of code for each property is always weird, and the second method is in. netformwork4.5 version support, Avoided a method of all the drawbacks of GG

Two methods of WPF data binding, interface refresh-----inotifypropertychanged

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.