Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1253
Create Data template column at run time
posted

Hi,

I saw this Xaml code in this forum:

<igGrid:XamWebGrid Name="xamWebGrid1">
            <igGrid:XamWebGrid.Columns>
                <igGrid:TextColumn Key="Value1">
                    <igGrid:TextColumn.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="1Some looooong text
      that should be split in multiline "
      TextWrapping="Wrap" Width="120"/>
                        </DataTemplate>
                    </igGrid:TextColumn.HeaderTemplate>
                </igGrid:TextColumn>
                <igGrid:TextColumn Key="SomeLongPropertyNameThatWillBeWrappedInTheGridHeader">
                    <igGrid:TextColumn.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}"
      TextWrapping="Wrap" Width="120"/>
                        </DataTemplate>
                    </igGrid:TextColumn.HeaderTemplate>
                </igGrid:TextColumn>

            </igGrid:XamWebGrid.Columns>
        </igGrid:XamWebGrid>

 

I would like to convert to C# but I don't know how I can add/insert a datatemplate to hold the textblock.


xamWebGrid1.Columns.Add(new TextColumn()
                                {
                                    Key = "Value1",
                                    HeaderText = "SomeLongPropertyNameThatWillBeWrappedInTheGridHeader",
                                    Width = new ColumnWidth(85, false),
                                    IsReadOnly = false,
                                   
                                });

Can anyone help?  Thanks!

Parents
  • 6759
    Offline posted

    Hi tangolp,

    You can set the HeaderTemplate for the TextColumn by using HeaderTemplate property of the TextColumn. So the code should look something like this:

    amWebGrid1.Columns.Add(new TextColumn()
                                    {
                                        Key = "Value1",
                                        HeaderText = "SomeLongPropertyNameThatWillBeWrappedInTheGridHeader",
                                        Width = new ColumnWidth(85, false),
                                        IsReadOnly = false,
                                        HeaderTemplate = YourDataTemplate
                                    });

    For the data template you have few options:

    • define DataTemplates that you are going to use in the resources of your page and access them at run time ( page.Resources[object key])
    • you could create the data template at runtime - check out this link for more details 
    • you can find another way to create your data template by checking out this thread 

    HTH

Reply Children