【问题标题】:How to convert below xaml Programmatically?如何以编程方式转换以下xaml?
【发布时间】:2012-07-16 05:58:05
【问题描述】:

我想要图像中的格式标题(第一行向内,总重量,纯重量和数量。第二行)。我可以使用 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 中看到,我已将网格放入内部并将标题分成两行。我想以编程方式从后面的代码中执行相同的方式。有人可以帮忙吗?

【问题讨论】:

    标签: c# .net wpf wpfdatagrid


    【解决方案1】:

    您可以尝试使用FrameworkElementFactory 以编程方式为您的标头创建DataTemplate,以下SO 线程具有与此相关的代码-How do I build a DataTemplate in c# code?

    但是,由于FrameworkElementFactorydeprecated,最好在Resources 中定义header 模板并使用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 但无法实现目标。你能给我提供相同的示例代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2019-12-12
    相关资源
    最近更新 更多