银光数据网

时间:2011-04-22 05:14:45

标签: silverlight datagrid

我很喜欢silverlight,所以我需要你身边的帮助。我的查询是一个haigng datagrid的页面,datagrid只有6列。之后6列他们是一个scape,因此scape显示自己一列。所以我避免datagrid.scape中的那列可能会显示出列,这是我的查询。 这对我来说很紧迫。请尽早解决问题。

1 个答案:

答案 0 :(得分:0)

虽然我不知道什么是'scape',但你需要做的是创建一个继承自IEditable和INotify的'display'类。例如:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Application.Views.DisplayClasses
{
    public class DisplayClass : IEditableObject, INotifyPropertyChanged
    {

       //Create private vars
        private string a;
        private string b;
        private string c;
        private string d;
        private bool e;


        // Create public properties with meta data to tell the grid to display and what order etc
        [Display(AutoGenerateField = false)]
        public string A
        {
            get { return a; }
            set { a = value; }
        }

        [Display(Order = 0, Name = "B", AutoGenerateField = true)]
        public string B
        {
            get { return b; }
            set { b = value; }
        }

        [Display(Order = 1, Name = "C", AutoGenerateField = true)]
        public String C
        {
            get { return c; }
            set { c= value; }
        }

        [Display(Order = 2, Name = "D", AutoGenerateField = true)]
        public string D
        {
            get { return d; }
            set { d = value; }
        }

        [Display(Order = 2, Name = "E", AutoGenerateField = true)]
        public string E
        {
            get { return e; }
            set { e = value; }
        }

        #region IEditableObject Members

        public void BeginEdit()
        {

        }

        public void CancelEdit()
        {

        }

        public void EndEdit()
        {

        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

然后,您需要创建一个ObservableCollection来存储从数据库中返回的数据:

    // Code to get data from the database (from your webservice)

//Make this Observable collection global
public static ObservableCollection<DisplayClass> ItemList = new ObservableCollection<DisplayClass>();

// In your oncompleted event method, put something similar to the following code
foreach (var DatabaseItem in DataFromMyWebService)
{
    DisplayClass GridItem = new DisplayClass();

    GridItem.A = DatabaseItem.A;
    GridItem.B = DatabaseItem.B;
    GridItem.C = DatabaseItem.C;
    GridItem.D = DatabaseItem.D;

    ItemList.Add(GridItem);
}

dgDataGrid.ItemsSource = ItemList;

您希望将可观察集合设为全局集合,以便在需要更改集合中的项目时,数据网格将自动显示这些更改。注意显示类中的元数据([])。这就是您控制显示哪些属性以及按什么顺序显示的方式。您还需要在XAML代码中的datagrid元素中设置属性“AutoGenerate =”True“。