CellTemplate Databinding

5 posts, 0 answers
  1. Johann Weder
    Johann Weder avatar
    1 posts
    Member since:
    Mar 2010

    Posted 13 May 2010 Link to this post

    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?
  2. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 13 May 2010 Link to this post

    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.
  3. Rob
    Rob avatar
    11 posts
    Member since:
    Nov 2009

    Posted 25 Oct 2010 Link to this post

    This worked excellent. Thanks.
  4. Thomas
    Thomas avatar
    17 posts
    Member since:
    Jan 2011

    Posted 14 Dec 2011 Link to this post

    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?
  5. Yuk
    Yuk avatar
    9 posts
    Member since:
    Aug 2010

    Posted 16 Jan 2012 Link to this post

    We have the same problem after upgrade to Q3 2011
Back to Top