silverlight資料繫結之DataGrid-轉載

來源:互聯網
上載者:User

原文地址:http://blog.163.com/zwx_gis/blog/static/32434435201132382957968/

註:所有代碼以C#為例

 

DataGrid繫結資料對象:

1、DataGrid的ItemsSource資料必須是對象List

2、DataGrid Column的Binding必須是對象的屬性

 

一、最基本的DataGrid綁定

1、前台

<sdk:DataGrid AutoGenerateColumns="True" Height="238" HorizontalAlignment="Left" Margin="170,12,0,0" Name="dataGridTest" VerticalAlignment="Top" Width="218" />

 

2、後台:

//定義類

        public class ContentData

        {

            public string SCHOOLID { get; set; }

            public string NAME { get; set; }

            public string SEX { get; set; }

        }

//執行個體化對象List

            List<ContentData> studentContentDataList = new List<ContentData>();

            for (int i = 0; i < 10; i++)

            {

                studentContentDataList.Add(new ContentData() {

                    SCHOOLID="201104"+i.ToString(),

                    NAME="學生"+i.ToString(),

                    SEX=(i<5)?"男D":"女?"

                });

            }

//綁定

dataGridTest.ItemsSource = studentContentDataList;

上面範例程式碼的結果如下:

 

 

 

二、在ArcGIS中,DataGrid可以綁定featureset。

可以採用將資料動態寫入自訂的featureset,然後綁定DataGrid,可實現DataGrid列數動態產生,避免採用上面類、對象List中屬性數寫死的缺點。

     代碼如下:

IList<Graphic> statisticFs = new List<Graphic>(); 

for(int i=0;i<10;i++)         //i為記錄數

{

statisticFs.Add(new Graphic());

         for (int j = 0; j < contentList.Count; j++)     //j為屬性數(列數)

         {

         statisticFs[i].Attributes.Add(contentList[j].NAME, tempTotalList[j]);

         }

}

    //綁定

    dataGridTest.ItemsSource = statisticFs;

    dataGridTest.Columns.Clear();

    for (int i = 0; i < contentList.count; i++)

    {

        DataGridTextColumn column = new DataGridTextColumn();

        column.Header = contentList[i].ALIAS;

        column.Binding = new System.Windows.Data.Binding("Attributes[" + contentList[i].NAME + "]");

        StatisticResult.Columns.Add(column);

     }

其中: List<ContentData> contentList = new List<ContentData>(); 

        public class ContentData

        {

            public string NAME { get; set; }

            public string ALIAS { get; set; }

        }

 

三、靜態繫結

DataGrid 的三種列:DataGridTextColumn、DataGridTemplateColumn、DataGridCheckBoxColumn

<DataGrid Name="dataGridColorPreview" AutoGenerateColumns="False" Height="221" HorizontalAlignment="Left" Margin="14,169,0,0" VerticalAlignment="Top" Width="304">

    <DataGrid.Columns>

        <DataGridTextColumn Header="編號" Binding="{Binding customId}" ></DataGridTextColumn>

        <DataGridTemplateColumn Header="顏色">

            <DataGridTemplateColumn.CellTemplate>

                <DataTemplate>

                    <Rectangle Width="100" Height="20" Fill="{Binding colorStr}" />

                </DataTemplate>

            </DataGridTemplateColumn.CellTemplate>

        </DataGridTemplateColumn>

        <DataGridTextColumn Header="種類(範圍)" Binding="{Binding range}" ></DataGridTextColumn>

    </DataGrid.Columns>

</DataGrid>  

 

四、動態綁定:XamlReader方法

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/

presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'  "+            "mlns:data='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data' " +

"   xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' ");

sb.Append("x:Name='tempGrid' >  ");

sb.Append("<data:DataGrid x:Name='grid1' Margin='1,1,1,1'

AutoGenerateColumns='False'>");

sb.Append("<data:DataGrid.Columns>");

sb.Append(" <data:DataGridTextColumn Width='160' Header='" + m_FieldName + "' Binding='{Binding Attributes[" + m_FieldsCNEN[m_FieldName] + "]}' />");

sb.Append(" </data:DataGrid.Columns>");

sb.Append("</data:DataGrid>");

sb.Append("  </Grid>  ");

Grid tempgrid = System.Windows.Markup.XamlReader.Load(sb.ToString()) as Grid;

 

又如:

using System.Windows.Data;
using System.Windows.Markup;
using System.Text;

...

DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = "Birthday";
StringBuilder CellTemp = new StringBuilder();
CellTemp.Append("<DataTemplate ");
CellTemp.Append("xmlns='http://schemas.microsoft.com/client/2007' ");
CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
//"YourNamespace" and "YourAssembly" 確保正確
CellTemp.Append("xmlns:local = 'clr-namespace:YourNamespace");
CellTemp.Append(";assembly=YourAssembly'>");
CellTemp.Append("<Grid>");
CellTemp.Append("<Grid.Resources>");
CellTemp.Append("<local:DateTimeConverter x:Key='DateConverter' />");
CellTemp.Append("</Grid.Resources>");
CellTemp.Append("<TextBlock ");
CellTemp.Append("Text = '{Binding Birthday, ");
CellTemp.Append("Converter={StaticResource DateConverter}}' ");
CellTemp.Append("FontFamily='Trebuchet MS' FontSize='11' ");
CellTemp.Append("Margin='5,4,5,4'/>");
CellTemp.Append("</Grid>");
CellTemp.Append("</DataTemplate>");
StringBuilder CellETemp = new StringBuilder();
CellETemp.Append("<DataTemplate ");
CellETemp.Append("xmlns='http://schemas.microsoft.com/client/2007' ");
CellETemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");
CellETemp.Append("<DatePicker ");
CellETemp.Append("SelectedDate='{Binding Birthday, Mode=TwoWay}' />");
CellETemp.Append("</DataTemplate>");
templateColumn.CellTemplate =
(DataTemplate)XamlReader.Load(CellTemp.ToString());
templateColumn.CellEditingTemplate =
(DataTemplate)XamlReader.Load(CellETemp.ToString());
targetDataGrid.Columns.Add(templateColumn);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.