PRISM-MVVM, ItemsControl problem with View Injection

Topics: Prism v2 - Silverlight 4, Prism v4 - Silverlight 4
Oct 26, 2010 at 9:16 AM

Hi,

I need to display multiple instances of a basketDetailsView.xaml within a region placed in basketView.xaml, but I'm getting the following errormessage when i debug my code:

"An exception occurred while creating a region with name 'basketRegion'. The exception was: System.InvalidOperationException: ItemsControl's ItemsSource property is not empty. This control is being associated with a region, but the control is already bound to something else. If you did not explicitly set the control's ItemSource property, this exception may be caused by a change in the value of the inherited RegionManager attached property"

The basketView XAML contains an ItemsControl tag defined like this:

<ItemsControl x:Name="basketItemsControl" BorderThickness="0" cal:RegionManager.RegionName="basketRegion" Margin="0,10,0,10" />

The view also has a listbox where I can uncheck/check the BasketDetailsViews I want to look at:

<ListBox x:Name="basketListBox" ItemsSource="{Binding basket}"  MinWidth="200">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <CheckBox commands:Checked1.Command="{Binding DataContext.CheckCommand,    ElementName=basketListBox}" Content="{Binding basketName}" ></CheckBox>
    </DataTemplate>       
  </ListBox.ItemTemplate>
</ListBox

 

When I run without debugging it executes fine and I can pop in/out the different basketDetailsViews, but when debugging the above mentioned error shows. What Am i doing wrong?
 

 

Oct 28, 2010 at 7:58 AM

Am I running into this issue? http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=213889

How can I solve it in my case?

Oct 28, 2010 at 6:32 PM
Edited Oct 28, 2010 at 6:32 PM

Hi,

First of all, please take into account that this is not related with the other thread. This exception is thrown in the Prism Library by design, more specifically in the ItemsControlsRegionAdpater class. This is because you have set previously the ItemsControl.ItemsSource property with some value. So, when the Adapt method of the region adapter is executed, it throws this exception.

It means that when you expose an ItemsControl as a region the ItemSource property is reserved, since it will be assigned with the view collection.

Hope this helps

Fernando Antivero

http://blogs.southworks.net/fantivero