将列表(类类型)与gridview绑定需要类的属性.为什么? [英] Binding of a list (of class type) with gridview needs the property from the class..why?

查看:73
本文介绍了将列表(类类型)与gridview绑定需要类的属性.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅示例!!!

see the example!!!

public class ClsProduct
   {
       public string pName;

       public int pPrice;

   }

           ClsProduct objclsProduct1=new ClsProduct();
           objclsProduct.pName="apple";
           objclsProduct.pPrice=140;
           ClsProduct objclsProduct2=new ClsProduct();
           objclsProduct.pName="orange";
           objclsProduct.pPrice=70;
           List<ClsProduct> lst=new List<clsProduct>();
           lst.add(objclsProduct1);
           lst.add(objclsProduct2);
           GridView1.DataSource = lst;
           GridView1.DataBind();




它不会工作!!!!


但是...






it wont work !!!!


but...



public class ClsProduct
    {
        public string pName
        {
          get;set; 
        }

        public int pPrice
        {
         get;set;
        }

    }

            ClsProduct objclsProduct1=new ClsProduct();
            objclsProduct.pName="apple";
            objclsProduct.pPrice=140;
            ClsProduct objclsProduct2=new ClsProduct();
            objclsProduct.pName="orange";
            objclsProduct.pPrice=70;

            List<ClsProduct> lst=new List<clsProduct>();
            lst.add(objclsProduct1);
            lst.add(objclsProduct2);
            GridView1.DataSource = lst();
            GridView1.DataBind();



这会工作!!!

这意味着gridview将绑定属性,但不绑定公共变量...
为什么???



this will work!!!

that means gridview will bind with properties but not with public variables...
Why ???

推荐答案

数据绑定只能通过依赖项属性来完成!请参考以下材料..

http://msdn.microsoft.com/en-us /library/ms752914.aspx

http://msdn.microsoft.com/en-us/library/ms752347.aspx


http://wpftutorial.net/DataBindingOverview.html
Databinding could be done only with dependency properties! Please refer the following materials..

http://msdn.microsoft.com/en-us/library/ms752914.aspx

http://msdn.microsoft.com/en-us/library/ms752347.aspx


http://wpftutorial.net/DataBindingOverview.html


因为属性和字段有很大不同:字段是直接可访问的变量,属性是通过方法调用来访问的(或现实中的两个-一个用于获取,另一个用于设置).进行数据绑定时,可以通过反射访问属性,而不是变量.

还有其他原因,涉及到INotifyPropertyChanged接口,但是它有点复杂,因此您最好阅读一本关于数据绑定的好书!除非您真的需要知道,否则请坚持将属性公开给数据绑定,而字段则不会这样.

无论如何,您都不应该将Fields公开! :D
Because Properties and fields are very different things: fields are directly accessible variables, properties are accessed via a method call (or two in reality - one for get and one for set). Properties are accessed via Reflection when the data binding takes place, variables aren''t.

There are other reasons, involving the INotifyPropertyChanged interface, but it gets kinda complicated so you would be best reading a good, think book on databinding! Unless you really need to know, just stick to the idea that Properties are exposed to databinding, and fields aren''t.

You shouldn''t be exposing Fields as public anyway! :D


这是因为您无法将数据与变量绑定".这是使用Properties有用的原因之一.

在此处阅读有关属性的信息:
MSDN:属性(C#编程指南) [为什么属性很重要 [属性与公共变量 [ ^ ]
This is because ''You can''t databind against a variable.'' This is one of the reasons why using Properties is helpful.

Read about properties here:
MSDN: Properties (C# Programming Guide)[^]
Why Properties Matter[^]

This is an interesting blog post: Properties vs. Public Variables[^]


这篇关于将列表(类类型)与gridview绑定需要类的属性.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆