1
votes

I have a UserControl that becomes a page in an XPS report. Part of the page is a table which is populated from a List. I have created a DataTemplate for each row of the table. However when i assign the Template to a ListView or ItemsControl i get the following error:

Error 2 An object of the type "System.Windows.DataTemplate" cannot be applied to a property that expects the type "System.Windows.Controls.ControlTemplate".

I have used this successfully in a Window, can the same not be done within a UserControl?

So in my UserControl.Resources i have the following data template

<DataTemplate x:Key="StiffenerTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="{Binding Path=Spacing}" />
            <TextBlock Grid.Column="1" Text="{Binding Path=Stiffener.Name}" />
            <TextBlock Grid.Column="2" Text="{Binding Path=XLeft}" />
            <TextBlock Grid.Column="3" Text="{Binding Path=XRight}" />
            <TextBlock Grid.Column="4" Text="{Binding Path=XLeft}" />
            <TextBlock Grid.Column="5" Text="{Binding Path=XRight}" />
        </Grid>
    </DataTemplate>

and in the main Grid of the Control i have

<ItemsControl ItemsSource="{Binding Path=WebStiffeners}" 
                          Template="{StaticResource StiffenerTemplate}"/>
1
Some kind of code r xaml ..... is required to get t know wkt you are trying to do.... - Ankesh
Appologies, That would be because i was trying to set the Template of the ItemControl rather than the ItemTemplate. All working now - Cadair Idris

1 Answers

2
votes

You need to use ItemTemplate instead of Template:

<ItemsControl ItemsSource="{Binding Path=WebStiffeners}" 
                          ItemTemplate="{StaticResource StiffenerTemplate}"/>

From MSDN ItemsControl:

ItemTemplate Gets or sets the DataTemplate used to display each item.

Template Gets or sets the ControlTemplate. The template that defines the appearance of the Control.