public void AddNewColumn(string path) { DataGridTemplateColumn column = new DataGridTemplateColumn() { Header = path, SortMemberPath=path }; StringBuilder sb = new StringBuilder(); sb.Append(@" <DataTemplate xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' > <TextBlock Tag='"); sb.Append(path); sb.Append(@"' Text='{Binding "); sb.Append(path); sb.Append(@"}' "); sb.Append(@"/> </DataTemplate>"); column.CellTemplate = XamlReader.Load(sb.ToString()) as DataTemplate; FlightPlanning.Columns.Add(column); }
要看高级的办法:请参见:Silverlight模拟WPF的TemplateSelector,以实现动态显示不同column