动态定义DataGrid列

首先定义Datagrid:

C# code
DataGrid targetDataGrid = new DataGrid(); targetDataGrid.ItemsSource = source; targetDataGrid.AutoGenerateColumns = false; LayoutRoot.Children.Add(targetDataGrid);



然后定义DataGrid的文本列

静态方法:

XML code
<data:DataGrid x:Name="targetDataGrid"> <data:DataGrid.Columns> <data:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" /> </data:DataGrid.Columns> </data:DataGrid>



动态方法:

XML code
using System.Windows.Data; ... DataGridTextColumn textColumn = new DataGridTextColumn(); textColumn.Header = "First Name"; textColumn.Binding = new Binding("FirstName"); targetDataGrid.Columns.Add(textColumn);



如果需要CheckBox列,可以定义如下:
静态方法:

XML code
<data:DataGrid x:Name="targetDataGrid"> <data:DataGrid.Columns> <data:DataGridCheckBoxColumn Header="Available " Binding="{Binding Available}" /> </data:DataGrid.Columns> </data:DataGrid>



动态方法:

C# code
using System.Windows.Data; ... DataGridCheckBoxColumn checkBoxColumn = new DataGridCheckBoxColumn(); checkBoxColumn.Header = "Available"; checkBoxColumn.Binding = new Binding("Available"); targetDataGrid.Columns.Add(checkBoxColumn);



如果需要定义模板列,

静态方法:

XML code
<UserControl.Resources> <local:DateTimeConverter x:Key="DateConverter" /></UserControl.Resources> ... <data:DataGrid x:Name="targetDataGrid" AutoGenerateColumns="False" > <data:DataGrid.Columns> <data:DataGridTemplateColumn Header="Birthday"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Birthday, Converter={StaticResource DateConverter}}" Margin="4"/> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <basics:DatePicker SelectedDate="{Binding Birthday, Mode=TwoWay}" /> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns></data:DataGrid>



动态方法定义模板列,方法有两种,一个是资源方法,另外一个是通过XAMLReader读取载入方法。

1. 资源方法:

XML code
<UserControl.Resources> <local:DateTimeConverter x:Key="DateConverter" /> <DataTemplate x:Key="myCellTemplate"> <TextBlock Text="{Binding Birthday, Converter={StaticResource DateConverter}}" Margin="4"/> </DataTemplate> <DataTemplate x:Key="myCellEditingTemplate"> <basics:DatePicker SelectedDate="{Binding Birthday, Mode=TwoWay}" /> </DataTemplate></UserControl.Resources>



C# code
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);



2. XamlReader载入方法

C# code
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/winfx/"); CellTemp.Append("2006/xaml/presentation' "); 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("Margin='4'/>"); CellTemp.Append("</Grid>"); CellTemp.Append("</DataTemplate>"); StringBuilder CellETemp = new StringBuilder(); CellETemp.Append("<DataTemplate "); CellETemp.Append("xmlns='http://schemas.microsoft.com/winfx/"); CellETemp.Append("2006/xaml/presentation' "); CellETemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' "); CellETemp.Append("xmlns:basics='clr-namespace:System.Windows.Controls;"); CellETemp.Append("assembly=System.Windows.Controls' >"); CellETemp.Append("<basics: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);

posted on 2012-08-01 12:23  [CC]  阅读(230)  评论(0)    收藏  举报

导航