我想要像图像中那样的格式标题(第一行中的 INWARD,第二行中的毛重,纯重和数量)。我可以使用 XAML 中的以下代码实现相同的目的,但我如何以编程方式执行此操作?
XAML:
<dg:DataGrid>
<dg:DataGridTemplateColumn Width="210">
<dg:DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="INWARD"
TextAlignment="Center">
</TextBlock>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Width="80"
Text="Gross Weight"
TextAlignment="Right"
Margin="0,0,2,0">
</TextBlock>
<TextBlock Width="80"
Text="Pure Weight"
TextAlignment="Right"
Margin="0,0,0,0">
</TextBlock>
<TextBlock Width="40"
Text="Quantity"
TextAlignment="Right"
Margin="2,0,0,0">
</TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</dg:DataGridTemplateColumn.HeaderTemplate>
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Style="{DynamicResource grdCellCurrencyData}"
Width="80"
Margin="0,0,2,0">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource CurrencyConverter}" ConverterParameter="True">
<Binding Path="INGrossWeight" Mode="OneWay" />
<Binding Path="BaseUOMNoofDecimals" Mode="OneWay" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Style="{DynamicResource grdCellCurrencyData}"
Width="80"
Margin="0,0,0 0">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource CurrencyConverter}" ConverterParameter="True">
<Binding Path="INPureWeight" Mode="OneWay" />
<Binding Path="BaseUOMNoofDecimals" Mode="OneWay" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Style="{DynamicResource grdCellCurrencyData}"
Width="40"
Text="{Binding Path=INQuantity, Mode=OneWay}" Margin="2,0,0,0">
</TextBlock>
</StackPanel>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid>
在上面的代码中,如果您在 DataGridTemplateColumn
中看到,我已经将网格放入其中并将标题分成两行。我想从后面的代码以编程方式进行操作的方式相同。有人可以帮忙吗?
最佳答案
你可以尝试使用FrameworkElementFactory
以编程方式为您的 header 创建 DataTemplate
,以下 SO 线程具有与此相关的代码 - How do I build a DataTemplate in c# code?
但是,因为 FrameworkElementFactory
是 deprecated , 最好在 Resources
中定义标题模板并使用 FindResource()
设置 HeaderTemplate。
编辑:
这是你的代码:
DataGridTemplateColumn col1 = new DataGridTemplateColumn();
//create the data template
DataTemplate headerLayout = new DataTemplate();
//set up the stack panel
FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
// define grid's rows
var row1 = new FrameworkElementFactory(typeof(RowDefinition));
gridFactory.AppendChild(row1);
var row2 = new FrameworkElementFactory(typeof(RowDefinition));
gridFactory.AppendChild(row2);
// set up the inwardTextBlock
FrameworkElementFactory inwardTextBlock = new FrameworkElementFactory(typeof(TextBlock));
inwardTextBlock.SetValue(Grid.RowProperty, 0);
inwardTextBlock.SetValue(TextBlock.TextProperty, "INWARD");
gridFactory.AppendChild(inwardTextBlock);
//set up the stack panel
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
spFactory.SetValue(Grid.RowProperty, 1);
// set up the grossWeightTextBlock
FrameworkElementFactory grossWeightTextBlock = new FrameworkElementFactory(typeof(TextBlock));
inwardTextBlock.SetValue(TextBlock.TextProperty, "Gross Weight");
inwardTextBlock.SetValue(TextBlock.WidthProperty, 80);
spFactory.AppendChild(inwardTextBlock);
// set up the pureWeightTextBlock
FrameworkElementFactory pureWeightTextBlock = new FrameworkElementFactory(typeof(TextBlock));
inwardTextBlock.SetValue(TextBlock.TextProperty, "Pure Weight");
inwardTextBlock.SetValue(TextBlock.WidthProperty, 80);
spFactory.AppendChild(inwardTextBlock);
// set up the qtyWeightTextBlock
FrameworkElementFactory qtyWeightTextBlock = new FrameworkElementFactory(typeof(TextBlock));
inwardTextBlock.SetValue(TextBlock.TextProperty, "Quantity");
inwardTextBlock.SetValue(TextBlock.WidthProperty, 80);
spFactory.AppendChild(inwardTextBlock);
gridFactory.AppendChild(spFactory);
// set the visual tree of the data template
headerLayout.VisualTree = gridFactory;
// set the header template
col1.HeaderTemplate = headerLayout;
关于c# - 如何以编程方式转换以下 xaml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11498796/