Problem of Implicit Data Template for Caliburn.Micro

Topics: Bugs, Conventions
Jun 28, 2012 at 11:43 AM

Hello everybody,

I'm encountering a problem when using an Implicit Data Template in Caliburn.Micro (version : 1.3.1) (Silverlight 5)

I have 3 model classes: 
- Facture: the root class 
- FactureAcompte: inherits from Facture
- FactureSejour: inherits from Facture

The view model contains a BindableCollection<Facture> (BindableCollection<Facture> FacturesImpayees).

My view contains a ListBox which binds to the bindable collection declared previously with caliburn-based convention

<ListBox x:Name="FacturesImpayees"

The bindable collection contains  FactureAcompte typed items and FactureSejour typed items. for that, I chose to use implicit data templates of SL5

Declaration of the data template:

<DataTemplate x:Key="FactureSejourDataTemplate"
<DataTemplate x:Key="FactureAcompteDataTemplate"
<DataTemplate x:Key="FactureDataTemplate"
Entities is the alias of the namespace in which are located the 
I always encounter the problem: cannot find view for ...FactureAcompte.
By trying to find the root cause of the problem in Caliburn.Micro, I found that Caliburn does not
consider the item templates that I declared , and it puts a default template.
If there any problem about what I made ? Could someone provide me with a correct implementation 
(a sample) of how to implement Implicit Data Templates in Silverlight with Caliburn.Micro conventions.
Jun 28, 2012 at 11:46 AM

The default convention for ItemsControl will use a default DataTemplate used to resolve the items view.

Either avoid the convention on the ItemsControl, or define the DataTemplate as specific views, using the proper naming convention (i.e. FactureView, FactureAcompteView and FactureSejourView).

Jun 28, 2012 at 12:47 PM

I tried the following scenarios:

- I defined the data templates in the <UserControl.Resources> side of the document
   * explicitly (by making the definition of the data template layout in the data template itself) --> Not OK
   * implicitly (by referring to view files : below (the same for FactureAcompteView and FactureSejourView) --> NOK

<DataTemplate DataType="Entities:Facture">
    <ReglementFacture:FactureView />

- I also tried to delcare this section if the Resources section of the ListBox --> NOK

    <DataTemplate DataType="Entities:Facture">
        <Entities:FactureView />

- I tried also to declare the Data template layout in the <ListBox.Resources> section of the ListBox in the xaml document --> NOK

Is there any solution to try other than these ?? Could you provide me with a sample of a listBox with Caliburn-based implicit data template ??

Thanks a lot in advance 

Jun 28, 2012 at 2:31 PM
Edited Jun 28, 2012 at 2:32 PM

My solution does not involve implicit DataTemplates at all. Nor I would use it, considering that CM does a pretty good job locating views from view models.

You are probably missing the point here: since if you define the ListBox using the naming conventions, like this:

<ListBox x:Name="FacturesImpayees"/>

you are relying on CM to locate views for specific items in the FacturesImpayees collection. Your DataTemplates will be utterly ignored, since the convention will set the ListBox.ItemTemplate, which will prevail on the implicit ones.

If you are willing to use implicit DataTemplates, then you should not use the naming convention, using a standard binding approach:

<ListBox ItemsSource="{Binding Path=FacturesImpayees}"/>

As I told you before, you can obtain the same effect as implicit DataTemplates (i.e. different views for each item of the collection), following the naming convention and declaring proper views. You don't need a DataTemplate at all.

I created a quick&dirty sample.