This is a migrated thread and some comments may be shown as answers.

CellTemplate not applied to dynamically added column

2 Answers 149 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 24 Aug 2011, 08:43 PM
I have RadGridView to which I am dynamically adding bound data columns.  I can successfully add columns as necessary, however, when I try to define a column with a CellTemplateSelector specified the selected CellTemplate is not applied to the column.  Through debugging the CellTemplateSelector I know that it is getting called and resolving the correct CellTemplate when the column is added, and the template applies correctly if used in the RadGridView XAML layout.  The pertinent code related to adding the column is as follows:
GridViewColumnCollection columns = this.gridView.Columns;
 
 ...elided...
 
    var col = (new GridViewDataColumn()
    {
        DataMemberBinding = new Binding(menuItem.BindingPath),
        Header = menuItem.Caption,
        CellTemplateSelector = new MemberCellTemplateSelector(),
    });
    columns.Add(col);

and the template is:
<local:MemberCellTemplateSelector.PhoneNumberList>
    <DataTemplate>
        <ItemsControl ItemsSource="{Binding MemberPhoneNumber}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                        <TextBlock Text="{Binding PhoneNumber}"/>
                        <TextBlock Text="{Binding PhoneNumberTypeKey, StringFormat='({0})'}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DataTemplate>
</local:MemberCellTemplateSelector.PhoneNumberList>


I have tried rebinding the grid, and calling ApplyTemplate() on both the grid, after adding the column, and on the cell passed to DataTemplateSelector.SelectTemplate() to no avail.  Am i missing something? How can I get the template to be applied to the column.




  

2 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 24 Aug 2011, 08:50 PM
... as an additional example, a separate column with the following template:
<local:MemberCellTemplateSelector.GenderType>
    <DataTemplate>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Gender Key:"/>
                <TextBlock Text="{Binding GenderTypeKey}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Description:"/>
                <TextBlock Text="{Binding TypeDescription}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
 
</local:MemberCellTemplateSelector.GenderType>
does not apply correctly when the column is added dynamically.  The template appears to apply partially; the first StackPanel in the template
<StackPanel Orientation="Horizontal">
                <TextBlock Text="Gender Key:"/>
                <TextBlock Text="{Binding GenderTypeKey}"/
</StackPanel>
appears to apply to the column data.

0
Pavel Pavlov
Telerik team
answered on 25 Aug 2011, 03:34 PM
Hello Chris,

I am not sure the template can render "partially" . Are you sure that the second stack panel is not cropped due to missing vertical space ?

All the best,
Pavel Pavlov
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Share this question
or