如何以编程方式转换下面的XAML?

3

我希望将页眉格式设置成如图所示(第一行为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,我已经将网格放在内部并将标题拆分为两行。同样的方式,我想从后台代码进行编程。有人可以帮忙吗?

1个回答

0
你可以尝试使用FrameworkElementFactory来编程创建DataTemplate作为你的标题,下面是一个相关代码的SO线程 - 如何在C#代码中构建DataTemplate? 但是,由于FrameworkElementFactory已经不推荐使用, 最好在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;

我尝试使用FrameworkElementFactory,但无法实现目标。你能否为我提供相应的示例代码? - Snehal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接