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

CellTemplate Databinding

4 Answers 285 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Johann Weder
Top achievements
Rank 1
Johann Weder asked on 13 May 2010, 09:28 AM
Hi,

I have three columns in a RadGridView that are of the same type (Resource) that should use the same DataTemplate. Initially I had the DataTemplate in the Grid.Resources section but could not get the DataBinding to work. Afterwards I used an elaborate way by specifing the template repeatedly and binding each under the RadGridView.Columns. This works but is obviously not the preferred way nor the elegant way to do this:

        <radControls:RadGridView x:Name="registerGridView" 
                                 AutoGenerateColumns="False">  
            <radControls:RadGridView.Columns> 
                <radControls:GridViewToggleRowDetailsColumn /> 
                <radControls:GridViewDataColumn Header="Client" /> 
                <radControls:GridViewDataColumn Header="Site" /> 
                <radControls:GridViewDataColumn Header="Post" /> 
                <radControls:GridViewDataColumn Header="Planned">  
                    <radControls:GridViewDataColumn.CellTemplate> 
                        <DataTemplate> 
                            <StackPanel Orientation="Horizontal">  
                                <TextBlock Text="{Binding PlannedResource.Firstname}" 
                                           Margin="2" /> 
                                <TextBlock Text="{Binding PlannedResource.Lastname}" 
                                           Margin="2" /> 
                            </StackPanel> 
                        </DataTemplate> 
                    </radControls:GridViewDataColumn.CellTemplate> 
                </radControls:GridViewDataColumn> 
                <radControls:GridViewDataColumn Header="Rostered">  
                    <radControls:GridViewDataColumn.CellTemplate> 
                        <DataTemplate> 
                            <StackPanel Orientation="Horizontal">  
                                <TextBlock Text="{Binding RosteredResource.Firstname}" 
                                           Margin="2" /> 
                                <TextBlock Text="{Binding RosteredResource.Lastname}" 
                                           Margin="2" /> 
                            </StackPanel> 
                        </DataTemplate> 
                    </radControls:GridViewDataColumn.CellTemplate> 
                </radControls:GridViewDataColumn> 
                <radControls:GridViewDataColumn Header="Actual">  
                    <radControls:GridViewDataColumn.CellTemplate> 
                        <DataTemplate> 
                            <StackPanel Orientation="Horizontal">  
                                <TextBlock Text="{Binding ActualResource.Firstname}" 
                                           Margin="2" /> 
                                <TextBlock Text="{Binding ActualResource.Lastname}" 
                                           Margin="2" /> 
                            </StackPanel> 
                        </DataTemplate> 
                    </radControls:GridViewDataColumn.CellTemplate> 
                </radControls:GridViewDataColumn> 
            </radControls:RadGridView.Columns> 
        </radControls:RadGridView> 

How do I create a single template and bind to the planned-, rostered- and actual resources?

4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 13 May 2010, 11:05 AM
Hello Johann Weder,

This can be done with a special column which will adjust the DataContext of the CellTemplate.

public class CustomColumn : GridViewBoundColumnBase
{
    public override System.Windows.FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {
        var element = base.CreateCellElement(cell, dataItem);
        element.DataContext = cell.Value;
  
        return element;
    }
}

Once you have such column you could write something similar to this:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="CustomTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Firstname}" Margin="2" />
                <TextBlock Text="{Binding Lastname}" Margin="2" />
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>
  
    <radControls:RadGridView>
        <radControls:RadGridView.Columns>
            <local:CustomColumn Header="Planned" 
                                            DataMemberBinding="{Binding PlannedResource}"
                                            CellTemplate="{StaticResource CustomTemplate}">
            </local:CustomColumn>
            <local:CustomColumn Header="Planned" 
                                            DataMemberBinding="{Binding RosteredResource}"
                                            CellTemplate="{StaticResource CustomTemplate}">
            </local:CustomColumn>
            <local:CustomColumn Header="Planned" 
                                            DataMemberBinding="{Binding ActualResource}"
                                            CellTemplate="{StaticResource CustomTemplate}">
            </local:CustomColumn>
        </radControls:RadGridView.Columns>
    </radControls:RadGridView>
</Grid>

Still, you are just going to have three separate DataTemplate, writing a custom column might not be justified.

Kind regards,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Rob
Top achievements
Rank 1
answered on 25 Oct 2010, 10:29 PM
This worked excellent. Thanks.
0
Thomas
Top achievements
Rank 1
answered on 14 Dec 2011, 04:24 PM
This is working as long as you don't use RowVirtualization. If EnableRowVirtualization is true, you get a random order when scrolling through the grid. Does anyone have an idea what we can do in this case?
0
Yuk
Top achievements
Rank 1
answered on 16 Jan 2012, 04:48 PM
We have the same problem after upgrade to Q3 2011
Tags
GridView
Asked by
Johann Weder
Top achievements
Rank 1
Answers by
Milan
Telerik team
Rob
Top achievements
Rank 1
Thomas
Top achievements
Rank 1
Yuk
Top achievements
Rank 1
Share this question
or