Silverlight数据绑定之DataGrid

时间:2011-08-03 01:59来源:网易博客 作者:Wilson. 点击:次
注:所有代码以C#为例 DataGrid绑定的数据对象: 1、DataGrid的ItemsSource数据必须是对象List 2、DataGrid Column的Binding必须是对象的属性 一、最基本的DataGrid绑定 1、前台 sdk:DataGridAutoGenerateColumns= True Height= 238 HorizontalAlignment= Left Margin= 170,12,0,0 Name= dataGridTest VerticalAlignment=
  

  注:所有代码以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);

  本文来自Wilson的博客,原文地址:http://blog.163.com/zwx_gis/blog/static/32434435201132382957968/

Silverlight数据绑定之DataGrid的更多相关文章

  1. Silverlight实例教程 – Datagrid,Dataform数据验证和ValidationSummary(转载)

    Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...

  2. Silverlight实用示例 - DataGrid行详细信息的绑定DataGrid.RowDetailsTemplate

    Silverlight实用示例 - DataGrid行详细信息的绑定DataGrid.RowDetailsTemplate 2012-12-28 21:04 来源:博客园 作者:chengxingli ...

  3. 【原创】有关Silverlight控件DataGrid的绑定数据后单元格单独复制的功能实现分析

    前些日子,公司新需求需要对silverlight的datagrid进行局部任意单元格数据可复制,查阅了半天网络资料愣是没找到相关资料,开始还以为是silverlight的bug根部无法实现, 最后还是 ...

  4. silverlight数据绑定模式TwoWay,OneWay,OneTime的研究

    asp.net开发中,数据绑定是一个很简单的概念,控件与数据绑定后,控件可以自动把数据按一定的形式显示出来.(当然控件上的值改变后,可以通过提交页面表单,同时后台服务端代码接收新值更新数据) silv ...

  5. silverlight 4中datagrid列标题和列内容居中问题,增加自增长列

    转载:http://www.cnblogs.com/guoyuanwei/archive/2011/01/02/1924163.html 命名空间:xmlns:Primitives="clr ...

  6. Silverlight之我见——DataGrid数据验证

    <UserControl x:Class="DataValidationSample.MainPage" xmlns="http://schemas.microso ...

  7. Silverlight数据绑定之 绑定一个int类型的属性

    还就真心不会啊! 在类FunctionPanel中作如下定义: /// <summary> /// 鼠标状态 属性 /// </summary> public Dependen ...

  8. silverlight数据绑定

    控件绑定 <Grid x:Name="LayoutRoot"> <StackPanel> <ScrollBar x:Name="bar&qu ...

  9. SilverLight: 数据绑定(1)-绑定到数据对象

    ylbtech-SilverLight-DataBinding: Binding to Data Objects(绑定到数据对象) 1.A, Building  a Data Object(创建一个数 ...

随机推荐

  1. ES6__class 的继承等相关知识案例

    /** * class 的继承等相关知识 */ // extends. static. super const canvas = document.querySelector('#canvas'); ...

  2. 进程Queue、线程Queue、堆栈、生产者消费者模型

    没学队列之前,可以用文件实现进程之间通信 但是有2个问题: 1. 速度慢:文件是保存在硬盘空间 2. 为了数据安全要加锁(处理锁是一件很麻烦的事,容易死锁,建议自己轻易不要处理锁) 队列:队列是基于管 ...

  3. python学习之 - configparser模块

    configparser模块功能:用于生成和修改常见配置文件.基本常用方法如下: read(filename):直接读取配置文件write(filename):将修改后的配置文件写入文件中.defau ...

  4. noip 2015 day1

    T1 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1 ...

  5. hdu 3943

    数位dp #include <cstdio> #include <cstdlib> #include <cmath> #include <stack> ...

  6. POJ 1062 【带约束的最短路问题】

    中文题题意不写. 建图: 我自己想到的建图方式是把每个物品看作两个点,编号分别是i和i+n,然后每个物品两个点之间边的权值是物品本身的价值.然后从第i个点往外连边,目标是可替代品顶点编号较小的点,权值 ...

  7. 2017多校Round2(hdu6045~hdu6055)

    补题进度:10/11 1001(不等式) 根据题意列不等式,解一解就行了 1002(套路) 题意: 给定一个随机产生的1e6*1e6的矩阵和一个1e3*1e3的矩阵,你要回答这个1e3*1e3的小矩阵 ...

  8. sql 2005 安装

    http://blog.csdn.net/wochuailimin/article/details/6120462 http://www.cnblogs.com/huangcong/archive/2 ...

  9. Linux信号通讯编程

    信号通讯流程为: ①进程A/内核选择信号 ②发送信号 ③进程B接收信号并处理 Linux系统支持的全部信号均定义在/usr/include/asm/signal.h.当中常见的信号有: ①SIGKIL ...

  10. Python访问MySQL数据库并实现其增删改查功能

    概述:对于访问MySQL数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删 ...