c# - 如何以编程方式转换以下 xaml?

标签 c# .net wpf wpfdatagrid

我想要像图像中那样的格式标题(第一行中的 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?

但是,因为 FrameworkElementFactorydeprecated , 最好在 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/

相关文章:

c# - 删除重复行

c# - 如何在 C# 中创建一个简单的代理?

c# - View 模型或XAML代码隐藏的WPF CollectionViewSource

.net - 在 WPF : Children. 中移除或 Children.Clear 不会释放对象

c# - MailKit IMAP 空闲 - 如何在 CountChanged 事件中访问 'done' CancellationTokenSource

c# - 使用扩展方法的 Selenium 并行测试

c# - .net 最小化 ssl 握手数据流量

c# - 从 ConcurrentDictionary 转换为 IDictionary

c# - 在 .NET 中捕获存储过程打印输出

c# - 通过 XAML 代码在 WPF TextBox 中制作多行文本