Silverlight Data Binding to DataGrid-reprint

Source: Internet
Author: User

Address: http://blog.163.com/zwx_gis/blog/static/32434435201132382957968/

Note: C # is used as an example for all codes.

 

Data Object bound to the DataGrid:

1. The itemssource data of the DataGrid must be the Object List

2. the binding of the DataGrid column must be an object attribute.

 

I. Basic DataGrid binding

1. Front-end

<SDK: DataGrid autogeneratecolumns = "true" Height = "238" horizontalalignment = "Left" margin = "17, 218 "name =" datagridtest "verticalignment =" TOP "width =" "/>

 

2. Background:

// Define a class

Public class contentdata

{

Public String schoolid {Get; set ;}

Public string name {Get; set ;}

Public String sex {Get; set ;}

}

// List of instantiated objects

List <contentdata> studentcontentdatalist = new list <contentdata> ();

For (INT I = 0; I <10; I ++)

{

Studentcontentdatalist. Add (New contentdata (){

Schoolid = "201104" + I. tostring (),

Name = "student" + I. tostring (),

Sex = (I <5 )? "Male d": "female? "

});

}

// Bind

Datagridtest. itemssource = studentcontentdatalist;

The result of the sample code above is as follows:

 

 

 

2. In ArcGIS, The DataGrid can be bound to featureset.

You can dynamically write data to a custom featureset and bind it to the DataGrid to dynamically generate the number of columns in the DataGrid. This avoids the disadvantage of overwrite the number of attributes in the top class and Object List.

The Code is as follows:

Ilist <graphic> statisticfs = new list <graphic> ();

For (INT I = 0; I <10; I ++) // I indicates the number of records.

{

Statisticfs. Add (new graphic ());

For (Int J = 0; j <contentlist. Count; j ++) // J indicates the number of attributes (number of columns)

{

Statisticfs [I]. Attributes. Add (contentlist [J]. Name, temptotallist [J]);

}

}

// Bind

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 );

}

Where: List <contentdata> contentlist = new list <contentdata> ();

Public class contentdata

{

Public string name {Get; set ;}

Public String alias {Get; set ;}

}

 

3. Static binding

The three columns of the DataGrid: datagridtextcolumn, datagridtemplatecolumn, and datagridcheckboxcolumn.

<DataGrid name = "maid" autogeneratecolumns = "false" Height = "221" horizontalalignment = "Left" margin = "14,169, 304" verticalignment = "TOP" width = "">

<DataGrid. Columns>

<Datagridtextcolumn header = "no." binding = "{binding customid}"> </datagridtextcolumn>

<Datagridtemplatecolumn header = "color">

<Datagridtemplatecolumn. celltemplate>

<Datatemplate>

<Rectangle width = "100" Height = "20" fill = "{binding colorstr}"/>

</Datatemplate>

</Datagridtemplatecolumn. celltemplate>

</Datagridtemplatecolumn>

<Datagridtextcolumn header = "type (range)" binding = "{binding range}"> </datagridtextcolumn>

</DataGrid. Columns>

</DataGrid>

 

4. dynamic binding: xamlreader Method

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'

Autogeneratecolumns = 'false'> ");

SB. append ("<data: DataGrid. Columns> ");

SB. append ("<data: datagridtextcolumn width = '000000' 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;

 

Another example:

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" make sure they are correct
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.