Silverlight动态生成DataGrid的几个方法
来源:互联网 发布:看排名的软件 编辑:程序博客网 时间:2024/05/16 13:50
http://www.silverlightchina.net/html/tips/2010/1023/2878.html
Silverlight动态生成DataGrid的几个方法
Defining a DataGrid
For any of these columns to be useful you are going to first need a DataGrid to add them to. The following creates a DataGrid, adds it as a child of the root layout Grid, and sets its ItemsSource to a collection called "source".
C#
DataGrid targetDataGrid = new DataGrid();targetDataGrid.ItemsSource = source;LayoutRoot.Children.Add(targetDataGrid);
VB
Dim targetDataGrid As New DataGridtargetDataGrid.ItemsSource = SourceLayoutRoot.Children.Add(targetDataGrid)
Defining a DataGrid Text Column
The following creates a DataGridTextBoxColumn bound to the FirstName property with a header of "First Name", and adds it to the columns collection of the DataGrid named "targetDataGrid".
Static
<my:DataGrid x:Name="targetDataGrid"><my:DataGrid.Columns><my:DataGridTextBoxColumn Header="First Name"DisplayMemberBinding="{Binding FirstName}" /></my:DataGrid.Columns></my:DataGrid>
Dynamic
C#
using System.Windows.Data;
...
DataGridTextBoxColumn textBoxColumn = new DataGridTextBoxColumn();textBoxColumn.Header = "First Name";textBoxColumn.DisplayMemberBinding = new Binding("FirstName");targetDataGrid.Columns.Add(textBoxColumn);
VB
Imports System.Windows.Data
...
Dim TextBoxColumn As New DataGridTextBoxColumnTextBoxColumn.Header = "First Name"TextBoxColumn.DisplayMemberBinding = New Binding("FirstName")TargetDataGrid.Columns.Add(TextBoxColumn)
Defining a DataGrid CheckBox Column
The following creates a DataGridCheckColumn bound to the Available property with a header of "Available", and adds it to the columns collection of the DataGrid named "targetDataGrid".
Static
<my:DataGrid x:Name="targetDataGrid"><my:DataGrid.Columns><my:DataGridCheckBoxColumn Header="Available "DisplayMemberBinding="{Binding Available}" /></my:DataGrid.Columns></my:DataGrid>
Dynamic
C#
using System.Windows.Data;
...
DataGridCheckBoxColumn checkBoxColumn = new DataGridCheckBoxColumn();checkBoxColumn.Header = "Available";checkBoxColumn.DisplayMemberBinding = new Binding("Available");targetDataGrid.Columns.Add(checkBoxColumn);
VB
Imports System.Windows.Data
...
Dim CheckBoxColumn As New DataGridCheckBoxColumnCheckBoxColumn.Header = "Available"CheckBoxColumn.DisplayMemberBinding = New Binding("Available")targetDataGrid.Columns.Add(CheckBoxColumn)
Defining a DataGrid Template Column
The following creates a DataGridTemplateColumn bound to the Birthday property with a header of "Birthday", and adds it to the columns collection of the DataGrid named "targetDataGrid".
Static
<UserControl.Resources><local:DateTimeConverter x:Key="DateConverter" /></UserControl.Resources>
...
<my:DataGrid x:Name="targetDataGrid" AutoGenerateColumns="False" ><my:DataGrid.Columns><my:DataGridTemplateColumn Header="Birthday"><my:DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlockText="{Binding Birthday,Converter={StaticResource DateConverter}}"FontFamily="Trebuchet MS" FontSize="11"Margin="5,4,5,4"/></DataTemplate></my:DataGridTemplateColumn.CellTemplate><my:DataGridTemplateColumn.CellEditingTemplate><DataTemplate><DatePickerSelectedDate="{Binding Birthday, Mode=TwoWay}" /></DataTemplate></my:DataGridTemplateColumn.CellEditingTemplate></my:DataGridTemplateColumn></my:DataGrid.Columns></my:DataGrid>
Dynamic
There are two ways to dynamically create a template column for a DataGrid. One is to load in the CellTemplate and CellEditingTemplates as DataTemplates from resources, and the other is to construct the DataTemplates on the fly using XamlReader.
1. Resources Method
This method creates the CellTemplate DataTemplate and the CellEditingTemplate DataTemplate in XAML and stores them as named resources. Then when the column is created the DataTemplates are used.
Use the below XAML to create the DataTemplates as resources to support the code for this method.
<UserControl.Resources><local:DateTimeConverter x:Key="DateConverter" /><DataTemplate x:Key="myCellTemplate"><TextBlockText="{Binding Birthday,Converter={StaticResource DateConverter}}"FontFamily="Trebuchet MS" FontSize="11"Margin="5,4,5,4"/></DataTemplate><DataTemplate x:Key="myCellEditingTemplate"><DatePickerSelectedDate="{Binding Birthday, Mode=TwoWay}" /></DataTemplate></UserControl.Resources>
C#
using System.Windows.Data;
...
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();templateColumn.Header = "Birthday";templateColumn.CellTemplate = (DataTemplate)Resources["myCellTemplate"];templateColumn.CellEditingTemplate =(DataTemplate)Resources["myCellEditingTemplate"];targetDataGrid.Columns.Add(templateColumn);
VB
Imports System.Windows.Data
...
Dim TemplateColumn As New DataGridTemplateColumnTemplateColumn.Header = "Birthday"TemplateColumn.CellTemplate = Resources("myCellTemplate")TemplateColumn.CellEditingTemplate = Resources("myCellEditingTemplate")targetDataGrid.Columns.Add(TemplateColumn)
2. XamlReader Method
This method creates the DataTemplate inline using the XamlReader class. This class takes a string and parses it to try to build a visual tree. In this case we are creating DataTemplates. This method is especially useful if the DataTemplate itself has to be dynamic. One example being if you wanted to modify what the element in the template was data bound to.
Warning: This method is considerably more difficult than the resources method. I recommend only using this if you need to dynamically create the DataTemplate.
Some things to watch out for:
- You will need to declare any XAML namespace that is used in the data template
- Any custom XAML namespace needs to specify both the clr-namespace and the assembly
- You cannot have white space between the xmlns: and the name of your namespace
- External resources cannot be referenced, they need to be declared inline
- The entire template is a single line, so if you get a XAML Parse exception, it will always say line 1, however the character is fairly accurate if you were to concatenate all of your lines.
- When using the StringBuilder approach shown below, make sure that you have the correct white space at the end of a line so that it is correctly separated when concatenated with the next line.
Now that the warnings are out of the way, here is how you do the equivalent of the code above:
C#
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' ");//Be sure to replace "YourNamespace" and "YourAssembly" with your app's//actual namespace and assembly hereCellTemp.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);
VB
Imports System.Windows.DataImports System.TextImports System.Windows.Markup
...
Dim TemplateColumn As New DataGridTemplateColumnTemplateColumn.Header = "Birthday"Dim CellTemp As New StringBuilderCellTemp.Append("<DataTemplate ")CellTemp.Append("xmlns = 'http://schemas.microsoft.com/client/2007' ")CellTemp.Append("xmlns:x = 'http://schemas.microsoft.com/winfx/2006/xaml' ")'Be sure to replace "YourNamespace" and "YourAssembly" with your app's'actual namespace and assembly hereCellTemp.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>")Dim CellETemp As New StringBuilderCellETemp.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 = XamlReader.Load(CellTemp.ToString())TemplateColumn.CellEditingTemplate = XamlReader.Load(CellETemp.ToString())targetDataGrid.Columns.Add(TemplateColumn)
- Silverlight动态生成DataGrid的几个方法
- silverlight datagrid动态生成列&动态绑定
- Silverlight 4 DataGrid动态生成列及绑定数据
- SilverLight ---动态生成DataGrid,动态绑定DataGrid模板列--With LinQ
- Silverlight datagrid 全选的另一种实现方法
- Silverlight动态添加DataGrid列
- DataGrid导出EXCEL的几个方法
- [原创]动态生成DataGrid的列
- 动态生成datagrid的绑定列
- 动态生成DataGrid的模板列 转帖
- Flex动态生成可编辑的DataGrid
- 完整的easyui datagrid 动态生成列
- Silverlight中DataGrid控件动态生成列并结合DataPager进行分页
- Silverlight中DataGrid控件动态生成列并结合DataPager进行分页
- Silverlight中DataGrid控件动态生成列并结合DataPager进行分页
- Silverlight中DataGrid控件动态生成列并结合DataPager进行分页二
- 动态生成datagrid列
- 动态生成 DataGrid列
- FL2440 Linux kernel + yaffs2根文件移植过程(二)
- DAO操作--以MySQL为例
- Red Hat Enterprise Linux Server release 6.0 --YUM 创库配置
- JAVA socket通信
- mini6410随笔
- Silverlight动态生成DataGrid的几个方法
- phone开发中第三方框架SDWebImage的简单使用
- Java路径问题最终解决方案
- qss 使用hover效果不能实现的奇怪问题
- POJ 2187 Beauty Contest 构造凸包 + 旋转卡壳
- 消灭Bug!十款免费移动应用测试框架推荐
- ArrayList to String[]
- 《程序员修炼之道--从小工到专家》第1-2章 读书心得
- 解决jetty运行时锁定文件问题