Dynamically specify RowDetailsTemplate at run-time

9 posts, 0 answers
  1. Alessandro
    Alessandro avatar
    31 posts
    Member since:
    Oct 2010

    Posted 11 Nov 2010 Link to this post

    I have a GridView that will be used for various types of objects like

    Schools
    Businesses
    Homes

    etc.

    I want the RowDetailsTemplate to vary based on the object type. I have each template defined as a separate user control. so, I have a SchoolsDetailTemplate.xaml, BusinessDetailTemplate.xaml and HomesDetailTemplate.xaml.

    What I need to do is to specify the RowDetailsTemplate to use at run-time. I can't seem to find a good example of how to do this. Any ideas?

     -- xavier
  2. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 12 Nov 2010 Link to this post

    Hello Xavier,

    This can be achieved by using a RowDetailsTemplateSelector as demonstrated in this sample.

    Hope it helps.


    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. DevCraft banner
  4. Alessandro
    Alessandro avatar
    31 posts
    Member since:
    Oct 2010

    Posted 12 Nov 2010 Link to this post

    Milan,

     Thanks for the reply. I'm not sure the DataTemplateSelector will work. We need  to specify the template to use at run-time based on the type of object being browsed. We would like to use reflection to query the type name (Person) and then instanciate the template by that name using the Activator and then assign it as the RowDetailsTemplate. Furthermore, the templates are defined in separate XAML files.

     Do you think the selector could be used in that way at run-time?

      Thanks!

    --- xavier

     



  5. Alessandro
    Alessandro avatar
    31 posts
    Member since:
    Oct 2010

    Posted 15 Nov 2010 Link to this post

    Milan,
     The ConditionalDataTemplateSelector ended up working out beautifully! Thanks. Basically, I used an object type converter as:

       public class ObjectTypeConverter : IValueConverter
        {
            #region IValueConverter Members

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value is Customer)
                    return 0;
                if (value is Employee)
                    return 1;
                if (value is Other)
                    return 2;

                return null;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    Thanks a bunch!  I'll mark this as answered and will post a follow-up question having do to with dynamically specifying columns based on the type. Thanks again!

    -- x
  6. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 30 May 2011 Link to this post

    Hello Milan,
    I have a template selector that is working.

    The problem I have is that I would like the template be changed dynamically in case I update the property it is linked too.

    public class ClientDetailDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var row = (GridViewRow)container;
            var obj = (Client)item;
     
            if (obj.IsStructure.HasValue && obj.IsStructure.Value == true)
            {
                return this.StructTemplate;
            }
            else
            {
                return this.PeopleTemplate;
            }
        }
     
        public DataTemplate PeopleTemplate
        {
            get;
            set;
        }
     
        public DataTemplate StructTemplate
        {
            get;
            set;
        }
    }

    For example here, the template is bound to the fact that IsStructure is checked or not.

    The property is bound to a check boxe inside a user control. This is my template declaration:

    <DataTemplate x:Key="PeopleItemTemplate">
                <my1:DetailPeople HorizontalAlignment="Left" x:Name="customerDetail" VerticalAlignment="Top" />
            </DataTemplate>
            <DataTemplate x:Key="StructItemTemplate">
                <my1:DetailStruct HorizontalAlignment="Left" x:Name="structureDetail" VerticalAlignment="Top" />
            </DataTemplate>
            <ogControls:ClientDetailDataTemplateSelector x:Key="myTemplateSelector" PeopleTemplate="{StaticResource PeopleItemTemplate}" StructTemplate="{StaticResource StructItemTemplate}" />

    The template does only change once I save the row, change row selction and come back. How can this be change as soon as the check boxe state changes?

    Thanks a lot for your help,
    John.
  7. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 06 Jun 2011 Link to this post

    Hello Xavier,

    Our latest release has a new feature which allows the grid to re-evaluate selectors when a property is changed. 



    Sincerely yours,
    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
  8. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 06 Jun 2011 Link to this post

    Hello Milan,

    Is there something special to do for the grid to re-evaluate the selector when my property changes ?
    Because I'm using version 2011.1.324.1040 and it does not work...
    I'm using RIA Services and my grid is bound to one of my entities, the usual.

    Any working sample somewhere? What am I missing?

    Thank you for your help,
    John.
  9. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 09 Jun 2011 Link to this post

    Hello John,

    Excuse me for this misunderstanding. We do reload all row and cell style selectors but not the row details one. 

    Will consider changing the behavior of the grid so that row details template selectors are updated as well. I will contact you as soon as I have more information.

    For the time being, whenever a property changes you can call the Rebind method of the grid. 


    All the best,
    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
  10. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 10 Jun 2011 Link to this post

    Hello Milan,
    Thank you for your help.
    Yes, it seems logical that the row details template selectors are updated as well.
    Thank you for considering adding this feature to a coming release.
    John.
Back to Top
DevCraft banner