积极答复者
这样的“DataGrid”应该怎样导出???

问题
-
这样的“DataGrid”应该怎样导出??? 将DataGrid中绑定数据导出为“Excel”和“Pdf”应该怎样做???
DataGrid定义如下:
<sdk:DataGrid Grid.Row="1" Name="dg_EnergyCustom" AutoGenerateColumns="False" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn DisplayIndex="-1" Header="年份" IsReadOnly="True" Width="30*" >
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock FontSize="13" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding EnergyYear}"></TextBlock>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="项目" Width="30*">
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Text="能耗量合计" Grid.Row="0" ></TextBlock>
<TextBlock Text="单位面积能耗" Grid.Row="1"></TextBlock>
<TextBlock Text="生均能耗" Grid.Row="2"></TextBlock>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="全校" Width="40*">
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding EnergySum}" ></TextBlock>
<TextBlock Grid.Row="1" Text="{Binding EnergyUnitArea}" ></TextBlock>
<TextBlock Grid.Row="2" Text="{Binding EnergyEveryOne}"></TextBlock>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>问题:
“DataGrid”中使用了“DataGridTemplateColumn”、“CellTemplate”和“DataTemplate”,并在它们中定义了“Grid”、“TextBlock”等元素。在这种情况下,“DataGrid”导出的数据和它展示的数据格式一致吗??? 导出方法应该如何写???
Science and technology is my lover.
- 已移动 ThankfulHeart 2012年7月26日 4:02 WPF问题 (发件人:Visual C#)
答案
-
你好,
在WPF中,我们用FrameworkElementFactory class来动态创建一个template,参考http://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelementfactory.aspx
一下代码作为参考:
private static DataTemplate GetToolTipsDataTemplate() { FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid)); FrameworkElementFactory rowDefinitionFactory1 = new FrameworkElementFactory(typeof(RowDefinition)); grid.AppendChild(rowDefinitionFactory1); FrameworkElementFactory rowDefinitionFactory2 = new FrameworkElementFactory(typeof(RowDefinition)); grid.AppendChild(rowDefinitionFactory2); FrameworkElementFactory rowDefinitionFactory3 = new FrameworkElementFactory(typeof(RowDefinition)); grid.AppendChild(rowDefinitionFactory3); FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock)); x.SetBinding(TextBlock.TextProperty, new Binding("X=")); rowDefinitionFactory1.AppendChild(x); FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock)); y.SetBinding(TextBlock.TextProperty, new Binding("Y=")); rowDefinitionFactory3.AppendChild(y); FrameworkElementFactory z = new FrameworkElementFactory(typeof(TextBlock)); z.SetValue(TextBlock.TextProperty, new Binding("Y=")); rowDefinitionFactory1.AppendChild(z); FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border)); border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black); border.AppendChild(grid); DataTemplate dt = new DataTemplate { VisualTree = border }; return dt; }
另一个方法:
private static DataTemplate GetToolTipsDataTemplate() { FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid)); FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock)); x.SetBinding(TextBlock.TextProperty, new Binding("X=")); grid.AppendChild(x); FrameworkElementFactory xValue = new FrameworkElementFactory(typeof(TextBlock)); xValue.SetValue(TextBlock.TextProperty, "{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"); grid.AppendChild(xValue); FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock)); y.SetBinding(TextBlock.TextProperty, new Binding("Y=")); grid.AppendChild(y); FrameworkElementFactory yValue = new FrameworkElementFactory(typeof(TextBlock)); yValue.SetValue(TextBlock.TextProperty, "{Binding Path=Values, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"); grid.AppendChild(yValue); FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border)); border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black); border.AppendChild(grid); DataTemplate dt = new DataTemplate {VisualTree = border}; return dt; }
希望对你有所帮助。
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Annabella LuoModerator 2012年8月3日 11:12
全部回复
-
如何动态的为“DataGrid”添加“列”??? 也就是通过后台代码添加列。。。
“DataGrid”定义如下:
<sdk:DataGrid Grid.Row="1" Name="dg_EnergyCustom" AutoGenerateColumns="False" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn DisplayIndex="-1" Header="年份" IsReadOnly="True" Width="30*" >
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock FontSize="13" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding EnergyYear}"></TextBlock>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="项目" Width="30*">
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Text="能耗量合计" Grid.Row="0" ></TextBlock>
<TextBlock Text="单位面积能耗" Grid.Row="1"></TextBlock>
<TextBlock Text="生均能耗" Grid.Row="2"></TextBlock>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="全校" Width="40*">
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding EnergySum}" ></TextBlock>
<TextBlock Grid.Row="1" Text="{Binding EnergyUnitArea}" ></TextBlock>
<TextBlock Grid.Row="2" Text="{Binding EnergyEveryOne}"></TextBlock>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>问题:
如何通过后台代码为“DataGrid”添加一列???列的静态XAML定义如下:
<sdk:DataGridTemplateColumn Header="电信学院" Width="40*">
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding EnergySum1}" ></TextBlock>
<TextBlock Grid.Row="1" Text="{Binding EnergyUnitArea1}" ></TextBlock>
<TextBlock Grid.Row="2" Text="{Binding EnergyEveryOne1}"></TextBlock>
</Grid>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>个人思路:
实例化“DataGridTemplateColumn”对象“DataGridTemplateColumn dgtc = new DataGridTemplateColumn();”,然后将“dgtc”添加到“DataGrid”中。
但是自己不知道,“CellTemplate”、“DataTemplate”这些元素怎样实例化,然后添加进入“ DataGridTemplateColumn”对象中。这样的功能应该怎么写???
Science and technology is my lover.
- 已移动 ThankfulHeart 2012年7月26日 4:03 WPF 问题 (发件人:Visual C#)
- 已合并 Annabella LuoModerator 2012年7月27日 6:38 same OP, same question
-
你好,
在WPF中,我们用FrameworkElementFactory class来动态创建一个template,参考http://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelementfactory.aspx
一下代码作为参考:
private static DataTemplate GetToolTipsDataTemplate() { FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid)); FrameworkElementFactory rowDefinitionFactory1 = new FrameworkElementFactory(typeof(RowDefinition)); grid.AppendChild(rowDefinitionFactory1); FrameworkElementFactory rowDefinitionFactory2 = new FrameworkElementFactory(typeof(RowDefinition)); grid.AppendChild(rowDefinitionFactory2); FrameworkElementFactory rowDefinitionFactory3 = new FrameworkElementFactory(typeof(RowDefinition)); grid.AppendChild(rowDefinitionFactory3); FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock)); x.SetBinding(TextBlock.TextProperty, new Binding("X=")); rowDefinitionFactory1.AppendChild(x); FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock)); y.SetBinding(TextBlock.TextProperty, new Binding("Y=")); rowDefinitionFactory3.AppendChild(y); FrameworkElementFactory z = new FrameworkElementFactory(typeof(TextBlock)); z.SetValue(TextBlock.TextProperty, new Binding("Y=")); rowDefinitionFactory1.AppendChild(z); FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border)); border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black); border.AppendChild(grid); DataTemplate dt = new DataTemplate { VisualTree = border }; return dt; }
另一个方法:
private static DataTemplate GetToolTipsDataTemplate() { FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid)); FrameworkElementFactory x = new FrameworkElementFactory(typeof(TextBlock)); x.SetBinding(TextBlock.TextProperty, new Binding("X=")); grid.AppendChild(x); FrameworkElementFactory xValue = new FrameworkElementFactory(typeof(TextBlock)); xValue.SetValue(TextBlock.TextProperty, "{Binding Path=[XValues], Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"); grid.AppendChild(xValue); FrameworkElementFactory y = new FrameworkElementFactory(typeof(TextBlock)); y.SetBinding(TextBlock.TextProperty, new Binding("Y=")); grid.AppendChild(y); FrameworkElementFactory yValue = new FrameworkElementFactory(typeof(TextBlock)); yValue.SetValue(TextBlock.TextProperty, "{Binding Path=Values, Converter={x:Static my:Converters.Format}, ConverterParameter=#.##}"); grid.AppendChild(yValue); FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border)); border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Black); border.AppendChild(grid); DataTemplate dt = new DataTemplate {VisualTree = border}; return dt; }
希望对你有所帮助。
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Annabella LuoModerator 2012年8月3日 11:12