欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程资源 > 编程问答 >内容正文

编程问答

inotifypropertychanged 接口 -凯发k8官方网

发布时间:2025/1/21 编程问答 19 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 inotifypropertychanged 接口 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

向客户端发出某一属性值已更改的通知。

名空间:system.componentmodel
程序集:  
system(在 system.dll 中)
语法public interface inotifypropertychanged
public interface class inotifypropertychanged
备注inotifypropertychanged
接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。

例如,考虑一个带有名为 firstname 属性的 person 对象。
若要提供一般性属性更改通知,则
person 类型实现 inotifypropertychanged 接口并在 firstname 更改时引发 propertychanged 事件。

若要在将客户端与数据源进行绑定时发出更改通知,则绑定类型应具有下列任一功能:

  • 实现 inotifypropertychanged 接口(首选)。

  • 为绑定类型的每个属性提供更改事件。

不执行上述这两个功能。

 

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.data.common;
using system.diagnostics;
using system.drawing;
using system.data.sqlclient;
using system.windows.forms;

// this form demonstrates using a bindingsource to bind
// a list to a datagridview control. the list does not
// raise change notifications, however the democustomer type 
// in the list does.
public class form1 : system.windows.forms.form
{
    
// this button causes the value of a list element to be changed.
    private button changeitembtn = new button();

    
// this datagridview control displays the contents of the list.
    private datagridview customersdatagridview = new datagridview();

    
// this bindingsource binds the list to the datagridview control.
    private bindingsource customersbindingsource = new bindingsource();

    
public form1()
    {
        
// set up the "change item" button.
        this.changeitembtn.text = "change item";
        
this.changeitembtn.dock = dockstyle.bottom;
        
this.changeitembtn.click  =
            
new eventhandler(changeitembtn_click);
        
this.controls.add(this.changeitembtn);

        
// set up the datagridview.
        customersdatagridview.dock = dockstyle.top;
        
this.controls.add(customersdatagridview);

        
this.size = new size(800200);
        
this.load  = new eventhandler(form1_load);
    }

    
private void form1_load(system.object sender, system.eventargs e)
    {
        
// create and populate the list of democustomer objects
        
// which will supply data to the datagridview.
        bindinglist<democustomer> customerlist = new bindinglist<democustomer>();
        customerlist.add(democustomer.createnewcustomer());
        customerlist.add(democustomer.createnewcustomer());
        customerlist.add(democustomer.createnewcustomer());

        
// bind the list to the bindingsource.
        this.customersbindingsource.datasource = customerlist;

        
// attach the bindingsource to the datagridview.
        this.customersdatagridview.datasource =
            
this.customersbindingsource;
    }

    
// change the value of the companyname property for the first 
    
// item in the list when the "change item" button is clicked.
    void changeitembtn_click(object sender, eventargs e)
    {
        
// get a reference to the list from the bindingsource.
        bindinglist<democustomer> customerlist =
            
this.customersbindingsource.datasource as bindinglist<democustomer>;

        
// change the value of the companyname property for the 
        
// first item in the list.
        customerlist[0].customername = "tailspin toys";
        customersbindingsource.resetitem(
0);
    }

    [stathread]
    
static void main()
    {
        application.enablevisualstyles();
        application.run(
new form1());
    }
}

// this is a simple customer class that 
// implements the ipropertychange interface.
public class democustomer  : inotifypropertychanged
{
    
// these fields hold the values for the public properties.
    private guid idvalue = guid.newguid();
    
private string customernamevalue = string.empty;
    
private string phonenumbervalue = string.empty;

    
public event propertychangedeventhandler propertychanged;

    
private void notifypropertychanged(string info)
    {
        
if (propertychanged != null)
        {
            propertychanged(
thisnew propertychangedeventargs(info));
        }
    }

    
// the constructor is private to enforce the factory pattern.
    private democustomer()
    {
        customernamevalue 
= "customer";
        phonenumbervalue 
= "(555)555-5555";
    }

    
// this is the public factory method.
    public static democustomer createnewcustomer()
    {
        
return new democustomer();
    }

    
// this property represents an id, suitable
    
// for use as a primary key in a database.
    public guid id
    {
        
get
        {
            
return this.idvalue;
        }
    }

    
public string customername
    {
        
get
        {
            
return this.customernamevalue;
        }

        
set
        {
            
if (value != this.customernamevalue)
            {
                
this.customernamevalue = value;
                notifypropertychanged(
"customername");
            }
        }
    }

    
public string phonenumber
    {
        
get
        {
            
return this.phonenumbervalue;
        }

        
set
        {
            
if (value != this.phonenumbervalue)
            {
                
this.phonenumbervalue = value;
                notifypropertychanged(
"phonenumber");
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/zhangtao/archive/2011/04/09/2010509.html

总结

以上是凯发k8官方网为你收集整理的inotifypropertychanged 接口的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图