Silverlight PRISM 4 TabControl items remove-add issue

Topics: Prism v4 - Silverlight 4
Jul 16, 2011 at 12:32 AM

Hello Team,
This issue happens only in PRISM 4 and works fine with PRISM 2.2 .
I have situation where I have the following XAML :-

<controls:TabControl  x:Name="tabControl"   Grid.Row="0"  VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5"  Regions:RegionManager.RegionName="MainRegion"  BorderThickness="1" Background="White">                <Regions:TabControlRegionAdapter.ItemContainerStyle>     
<Style TargetType="controls:TabItem">   
                <Setter Property="HeaderTemplate">             
<TextBlock MinWidth="45" MaxWidth="50" Text="{Binding Text}" Margin="5" />   

I add a TextBlock in the code behind as follows :-

 _RegionManager.RegisterViewWithRegion("MainRegion", ()=> new TextBlock { Text = "First Tab", Height = 200 });

And I remove the tabitem from the TabControl as :-

So after I add an item(s) using above code and then remove an item and then again add an item, I get the following error :-

{System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.Parameter name: index   at System.Windows.PresentationFrameworkCollection`1.InsertImpl(Int32 index, Object value)   at System.Windows.Controls.ItemCollection.InsertImpl(Int32 index, Object value)   at System.Windows.Controls.ItemCollection.InsertInternal(Int32 index, Object value)   at System.Windows.PresentationFrameworkCollection`1.Insert(Int32 index, T value)   at Microsoft.Practices.Prism.Regions.Behaviors.TabControlRegionSyncBehavior.OnViewsChanged(Object sender, NotifyCollectionChangedEventArgs e)   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)   at Microsoft.Practices.Prism.Regions.ViewsCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e)   at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(IList items, Int32 newStartingIndex)   at Microsoft.Practices.Prism.Regions.ViewsCollection.NotifyAdd(Object item)   at Microsoft.Practices.Prism.Regions.ViewsCollection.SourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)   at System.Collections.ObjectModel.Collection`1.Add(T item)   at Microsoft.Practices.Prism.Regions.Region.InnerAdd(Object view, String viewName, IRegionManager scopedRegionManager)   at Microsoft.Practices.Prism.Regions.Region.Add(Object view, String viewName, Boolean createRegionManagerScope)   at Microsoft.Practices.Prism.Regions.Region.Add(Object view)   at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.AddViewIntoRegion(Object viewToAdd)   at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(Object sender, ViewRegisteredEventArgs e)}


Please guide me.

Jul 18, 2011 at 1:48 PM


The problem you're experiencing might be caused by the fact that you're modifying your region's hosting control's items directly, instead of interacting with the region.

When you define a region in a control, the corresponding region adapter wraps the control in a region, and it is expected that you manipulate the contents of that region through the region, not through the control itself.

Therefore, you could try using the Region.Remove method to remove the items from your region.

Additionally, you should note that the RegisterViewWithRegion method (view discovery) does not add a view to a region, but rather registers that view with the region, so that, when a region with that name is created, the registered view is added to it. In order to programatically add a view to a region, the recommended approach is to use the Region.Add method (view injection).

You can read more about this in the UI Composition chapter from the Prism MSDN documentation.

I hope you find this helpful.

Guido Leandro Maliandi