Silverlight - DataGrid Binding 資料

2010/03/24

很簡單但是紀錄一下..

首先我先建立一個Class ..


C#:


  public class Order
  {
        public string OrderNum { get; set; }
        public string ProductName { get; set; }
        public string UserName { get; set; }
        public string ExpireData { get; set; }
        public int Price { get; set; }
  }






這時候 我們在XMAL那邊建立一個Datagrid.

XMAL:


  <data:DataGrid AutoGenerateColumns="False" Height="227" HeadersVisibility="All"
                    RowBackground="Cornsilk" AlternatingRowBackground="LemonChiffon"
                    HorizontalAlignment="Left" Margin="234,368,0,0"
                    Name="dataGrid1" VerticalAlignment="Top" Width="569">

            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="訂單編號"
                Binding="{Binding OrderNum}" />

                <data:DataGridTextColumn Header="訂購人"
                Binding="{Binding UserName}" />

                <data:DataGridTextColumn Header="產品名稱" Width="50"
                Binding="{Binding ProductName}" />
                <data:DataGridTextColumn Header="價錢"
                Binding="{Binding Price}" />
            </data:DataGrid.Columns>

        </data:DataGrid>



這時候我們就可以讓他對於這Order 這 Class進行 資料的Binding..

這很簡單很多書上有教,而且方法跟winform 還有 ASP.net都差不多..

Binding Code:



            OrderPool = new List<Order>();

            Order a = new Order();
            a.OrderNum = "1";
            a.ExpireData = "2009/12/20";
            a.Price = 1000;
            a.ProductName = "Ipod Touch";
            a.UserName = "Dracula";

            OrderPool.Add(a);

            Order a1 = new Order();
            a1.OrderNum = "2";
            a1.ExpireData = "2009/1/20";
            a1.Price = 2000;
            a1.ProductName = "Appla iPad";
            a1.UserName = "Lucifer";

            OrderPool.Add(a1);

            this.dataGrid1.ItemsSource = OrderPool;


0 意見:

程式 . 生活 . D小調.@2010 | Binary Design: One Winged Angel.