[Windows Forms]: bindingsource usage mode-Data Binding BASICS (1)

Source: Internet
Author: User

 

Preface:

In the article "[. Net]: bindingsource usage mode-dynamic drop-down menu (pure IDE development )」.

 

This section describes how to use Visual Studio ide to develop a dynamic drop-down menu.
With the IDE development mode, you can quickly establish the user interfaces required by the project.
However, when we need to have higher control over the details, this development mode will become increasingly unavailable.
In this case, you need to use the program code development mode to develop and design data binding.

 

This article briefly introduces several objects used for Data Binding design and development.
Allows software developers to have a basic understanding of the object operation mode when designing data binding-related program code.

 

Inotifypropertychanged:

Inotifypropertychanged only defines an event "propertychanged 」.
The object implementing inotifypropertychanged will trigger the propertychanged event when the property value is changed.

 

Related information: inotifypropertychanged Interface

 

Propertydescriptor:

Propertydescriptor encapsulates the attributes of a class.

 

For example, if there is a category, we need to list its public attributes.
In this case, the propertydescriptor object set of this category can be obtained through system. componentmodel. typedescriptor. getproperties.
Each propertydescriptor object in this propertydescriptor object set is the encapsulation of all open attributes of this category.

 

Developers can obtain information about this attribute through the propertydescriptor object.

using System;using System.ComponentModel;namespace ConsoleApplication1{    public class County     {        // Properties        public int CountyID { get; private set; }        public string CountyName { get; set; }        public string CountyDescription { get; set; }        // Constructor        public County(int countyID)        {            this.CountyID = countyID;            this.CountyName = string.Empty;            this.CountyDescription = string.Empty;        }    }        class Program    {        static void Main(string[] args)        {            foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(typeof(County)))            {                Console.WriteLine(string.Format("Name={0}; IsReadOnly={1};", propertyDescriptor.Name, propertyDescriptor.IsReadOnly));            }            Console.ReadLine();        }    }}

 

In addition, developers can use the getvalue method of the propertydescriptor object to obtain the value of the object instance of this attribute.

Using system; using system. componentmodel; namespace consoleapplication1 {public class County {// properties public int countyid {Get; private set;} Public String countyname {Get; set;} Public String countydescription {Get; set ;} // constructor public County (INT countyid) {This. countyid = countyid; this. countyname = string. empty; this. countydescription = string. empty ;}} class program {static void main (string [] ARGs) {County county = new county (1); County. countyname = "Taipei City"; County. countydescription = "cannot afford"; foreach (propertydescriptor in typedescriptor. getproperties (typeof (county) {console. writeline (string. format ("name = {0}; value = {1};", propertydescriptor. name, propertydescriptor. getvalue (county);} console. readline ();}}}

 

Of course, developers can also set the value of the object instance of this attribute through the setvalue method of the propertydescriptor object.

Using system; using system. componentmodel; namespace consoleapplication1 {public class County {// properties public int countyid {Get; private set;} Public String countyname {Get; set;} Public String countydescription {Get; set ;} // constructor public County (INT countyid) {This. countyid = countyid; this. countyname = string. empty; this. countydescription = string. empty ;}} class program {static void main (string [] ARGs) {County county = new county (1); County. countyname = "Taipei City"; County. countydescription = "cannot afford"; // getpropertydescriptor propertydescriptor = typedescriptor. getproperties (typeof (county) ["countydescription"]; // setvalue propertydescriptor. setvalue (County, "one day can afford"); // result console. writeline (string. format ("value = {0};", County. countydescription); console. readline ();}}}

 

The propertydescriptor object is also implemented, and the function is notified when the attribute value is changed.
Developers can use addvaluechanged to assign the attributes to be executed when their values change.

Using system; using system. componentmodel; namespace consoleapplication1 {public class County {// properties public int countyid {Get; private set;} Public String countyname {Get; set;} Public String countydescription {Get; set ;} // constructor public County (INT countyid) {This. countyid = countyid; this. countyname = string. empty; this. countydescription = string. empty ;}} class program {static void main (string [] ARGs) {County county = new county (1); County. countyname = "Taipei City"; County. countydescription = "cannot afford"; // getpropertydescriptor propertydescriptor = typedescriptor. getproperties (typeof (county) ["countydescription"]; // addvaluechanged eventhandler valuechangeddelegate = delegate (Object sender, eventargs e) {console. writeline (string. format ("valuechanged value = {0};", (county) sender ). countydescription);}; propertydescriptor. addvaluechanged (County, valuechangeddelegate); // setvalue propertydescriptor. setvalue (County, "one day can afford"); // result console. writeline (string. format ("value = {0};", County. countydescription); console. readline ();}}}

 

In addition, it is worth mentioning that if the category is actually implemented with the inotifypropertychanged interface.
When the program raises the inotifypropertychanged. propertychanged event, the delegates added through addvaluechanged will also be executed.

Using system; using system. componentmodel; namespace consoleapplication1 {public class county: inotifypropertychanged {// properties private int _ countyid = int. minvalue; private string _ countyname = string. empty; private string _ countydescription = string. empty; // constructor public County (INT countyid) {_ countyid = countyid; _ countyname = string. empty; _ countydescription = string. empty;} // PR Operties public int countyid {get {return _ countyid;} set {If (_ countyid! = Value) {_ countyid = value; this. onpolicypropertychanged ("countyid") ;}} Public String countyname {get {return _ countyname;} set {If (_ countyname! = Value) {_ countyname = value; this. onpolicypropertychanged ("countyname") ;}} Public String countydescription {get {return _ countydescription;} set {If (_ countydescription! = Value) {_ countydescription = value; this. onnotifypropertychanged ("countydescription") ;}}// event public event propertychangedeventhandler propertychanged; protected void onnotifypropertychanged (string propertyname) {# region require if (string. isnullorempty (propertyname) = true) throw new argumentnullexception (); # endregion if (this. propertychanged! = NULL) {This. propertychanged (this, new propertychangedeventargs (propertyname) ;}} class program {static void main (string [] ARGs) {County county County = new county (1); County. countyname = "Taipei City"; County. countydescription = "cannot afford"; // getpropertydescriptor propertydescriptor = typedescriptor. getproperties (typeof (county) ["countydescription"]; // addvaluechanged eventhandler valuechangeddelegate = delegate (Object sender, eventargs e) {console. writeline (string. format ("valuechanged value = {0};", (county) sender ). countydescription);}; propertydescriptor. addvaluechanged (County, valuechangeddelegate); // setvalue County. countydescription = "one day can afford"; // result console. writeline (string. format ("value = {0};", County. countydescription); console. readline ();}}}

 

Related materials: propertydescriptor category and typedescriptor category

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.