原文地址: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);