Problem with Dependency Injection (Prism 4) and DataTemplate that contains toolkit controls

Topics: Prism v4 - Silverlight 4
Dec 8, 2010 at 3:20 PM

Hello,

I ran into a problem that seems to be happening when I use dependency injection via Prism (version 4)  with a view that contains DataTemplate that has DockPanel inside it (or any control from the toolkit namespace). When the view is instantiated by the container, I get an exception that the namespace that contains the DockPanel does not exist, even though in the designer everything is fine. What makes this even less understood, is that if I add DockPanel to the user control (in addition to the one in the DataTemplate) then everything is fine, both DockPanels are shown.

Here is how the code basically looks:

  •  ·         The namespace used for DockPanel was added by Visual Studio when I dragged it from the toolbox, and is defined like this:

      xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"

  • ·         In the user control the DataTemplate is defined as follows:

 <UserControl.Resources>

    <DataTemplate x:Key="JobItemDataTemplate">
         <Border BorderBrush="Black" BorderThickness="0,0,0,1">
             <Grid>
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*"/>
                      <ColumnDefinition Width="100"/>
                 </Grid.ColumnDefinitions>
                 <toolkit:DockPanel Grid.Column="0">
                      <TextBlock Text="{Binding Name, Mode=TwoWay}" FontWeight="Bold" toolkit:DockPanel.Dock="Top"/>
                      <TextBlock Text="{Binding Description, Mode=TwoWay}" Height="24" toolkit:DockPanel.Dock="Top"/>
                 </toolkit:DockPanel>

                   </Grid>
             </Border>
    </DataTemplate>
</UserControl.Resources>

 

  • ·         Then I have a ListBox in the user control itself that uses the above template:

 <Grid x:Name="LayoutRoot" Background="White">

    <ListBox x:Name="m_jobsListBox" ItemTemplate="{StaticResource JobItemDataTemplate}" 
   </ListBox>
</Grid>

 

  • ·         In my ModulInit, I register my user control within a region like this:
m_regionManager.RegisterViewWithRegion( RegionNames.JobsListRegion, () => m_container.Resolve<JobsListView>() );
 
  • ·         In run time, I get the following exception:
"The type 'DockPanel' was not found because 'http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit' is an unknown namespace."
 
  • ·         If I add the following line after the ListBox, then everything is fine! -
<toolkit:DockPanel Height="100" HorizontalAlignment="Left" Margin="33,188,0,0" Name="dockPanel1" VerticalAlignment="Top" Width="200" Background="#FF00D600" />
 
  • · The problems happens with any control defined in the toolkit namespace, not just the DockPanel.
  • I tried cleaning the solution, removing the DockPanel and inserting it again (for possible references problem), and it didn't help
  • This also works if I don't use Prism and create the user control without dependency injection (plain silverlight 4 application).
  
Any help would be welcome.
Thanks!
Ilan
Developer
Dec 9, 2010 at 7:24 PM

Hi Ilan,

You could try adding a reference to the Toolkit assemblies you are using in your Shell project.

Let me know if you keep experiencing this problem after adding the aforementioned reference.

You might also find this thread useful.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Dec 12, 2010 at 8:05 AM

Hi Guido,

Yes, adding the reference to the shell project indeed solved the problem, thanks!

From the link that you sent it seems that if I use control with data template I manually need to add references it uses to the application project. Surprising.

Thank you for the quick response and for resolving this issue for us!

Ilan