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

Dynamically specify RowDetailsTemplate at run-time

8 Answers 192 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alessandro
Top achievements
Rank 1
Alessandro asked on 12 Nov 2010, 01:16 AM
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

8 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 12 Nov 2010, 08:52 AM
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
0
Alessandro
Top achievements
Rank 1
answered on 12 Nov 2010, 03:43 PM
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

 



0
Alessandro
Top achievements
Rank 1
answered on 15 Nov 2010, 08:07 PM
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
0
Jonx
Top achievements
Rank 2
answered on 31 May 2011, 03:28 AM
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.
0
Milan
Telerik team
answered on 06 Jun 2011, 09:02 AM

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
0
Jonx
Top achievements
Rank 2
answered on 06 Jun 2011, 09:18 AM
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.
0
Milan
Telerik team
answered on 09 Jun 2011, 12:22 PM
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
0
Jonx
Top achievements
Rank 2
answered on 10 Jun 2011, 07:19 PM
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.
Tags
GridView
Asked by
Alessandro
Top achievements
Rank 1
Answers by
Milan
Telerik team
Alessandro
Top achievements
Rank 1
Jonx
Top achievements
Rank 2
Share this question
or