Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Assume I have an object as such

public interface ICustomObj
{
  ....
}

public class CustomObj<t> : ICustomObj
{
 //Impliments ICustomObj
}
</t>


I have a different object that has a collection of the CustomObj's and they have different types (e.g. some have strings and some have Date Time values. I would like to define a different data template for the type
CustomObj<string></string>
and for
CustomObj<datetime></datetime>


Can this be done?

[EDIT]

In an attempt to use the converter I am stumbling on how to pass the generic value to the converter...

I want to due something like this:

XML
<ListBox ItemsSource="{Binding Path=EntryItems}">
 <ListBox.ItemTemplate>
   <DataTemplate Template="{Binding Path=Value, Converter=EntryConverter}"/>
 </ListBox.ItemTemplate>
</ListBox>




But get the error
VB
The property 'Template' cannot be set as a property element on template. Only Triggers and Storyboards are allowed as property elements.


[EDIT]
Solution is to use a DataTemplateSelector. An easy example can be found Here[^]
Posted
Updated 8-Jul-11 3:44am
v4
Comments
Mark Salsbery 21-Jun-11 15:07pm    
For what it's worth, generics support in XAML is coming someday...Generics in XAML[^]
[no name] 21-Jun-11 15:18pm    
Good to know! Thank you!

There's no direct support for hooking a DataTemplate to a generic type. However, the generally accepted way of working around this is to use customer MarkupExtensions to add this capability. You can find more on this here[^] and here[^].
 
Share this answer
 
Comments
[no name] 21-Jun-11 9:53am    
Can't get to the second link (blocked). The first one makes sence but seems to be a lot of work. Guess I will go with the converter as another solution suggests... Thank you!
Pete O'Hanlon 21-Jun-11 10:05am    
There is another way - although it's a hack. You could always create a concrete implementation of the generic class, e.g. public class CustomString : CustomObj<string> { }
[no name] 21-Jun-11 15:17pm    
Oi.... Yeah I don't like that. Thanks though.
No need to Create different DataTemplate for this. Use IValueConverter and convert the value based on the type of the object e.g. if object.GetType() == DateTime etc.

Return the content from the Convert.
 
Share this answer
 
Comments
[no name] 21-Jun-11 9:53am    
So I assume the converter needs access to all of the DataTemplates then?
nit_singh 21-Jun-11 10:21am    
yes If you want to customize the Content, ToolTip or any property, then create a seprate Converter for all the templates.
[no name] 7-Jul-11 16:47pm    
So I was working on something else the last couple weeks and finally got back to this... I am not sure how I can pass the value to the object parameter of the converter.
If I try to set the Template I get the error Error "The property 'Template' cannot be set as a property element on template. Only Triggers and Storyboards are allowed as property elements."

So I am confused as to how I would use the converter....
Rather than using a converter a DataTemplateSelector made more sence. I found an easy example Here[^] that helped me build mine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900