Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8237027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:20:04+00:00 2026-06-07T19:20:04+00:00

I want format Header like in image (INWARD in First Line, Gross Wt., Pure

  • 0

I want format Header like in image (INWARD in First Line, Gross Wt., Pure Wt. & Qty. in Second Line). I can achieve same with following code in XAML, but how can I do this Programmatically?

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>

In above code if you see in DataGridTemplateColumn, I have taken grid inside and split header in two rows. Same way I want to do programmatically from code behind. can anybody help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:20:05+00:00Added an answer on June 7, 2026 at 7:20 pm

    You can try and use FrameworkElementFactory to create the DataTemplate for your header programatically, following SO thread is having code related to that – How do I build a DataTemplate in c# code?

    But, as FrameworkElementFactory is deprecated, it would be better to define header template in Resources and use FindResource() to set HeaderTemplate.

    Edit:

    here is your code:

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create JSON , format is like "header": { "b":"aaaa", "c":"00", "d":"zzzzz",
I want to format a date object so that I can display strings such
I want to format a datetime field but i can't figure out the syntactic.
I have seen this web beacon image here The code looks like this: header(
All I want to do is an Image to the validation summary and format
i want to format number entered by user in dutch format. ie. use decimal
I want to format a string before assigning it to a label's text property
I want to format a NSDecimalNumber as a Dollars value ($1.50) but Im getting
i want to format date to string in hql select, for example i have
I want to format a number in indian format. for example, x= 123456 should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.