Ok, here's my simple scenario. I've got collection of strings that I'm binding to a TabControl as a proof of concept. As I add strings I want a new tab with the region name as the header and a ItemsControl in the Tab container. That ItemsControl should define a new region.
<TabControl x:Name="tabDemo" ItemsSource="{Binding DynamicRegions}" >
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ItemsControl cal:RegionManager.RegionName="{Binding}" ItemsSource="{x:Null}">
</ItemsControl>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
From there I add strings to the collection. The tabs show up fine, but when I try to call
private void AddDynamicRegion(object arg)
{
var newRegionName = "Region" + (DynamicRegions.Count + 1).ToString();
DynamicRegions.Add(newRegionName);
}
private void AddRandomRegionContent(object arg)
{
if (string.IsNullOrEmpty(SelectedRegion) )
return;
Debug.WriteLine("Injected " + RegionContent + " into " + SelectedRegion);
var newContent = new TextBlock() { Text = RegionContent };
_regionManager.RegisterViewWithRegion(SelectedRegion,() => newContent );
_regionManager.Regions[SelectedRegion].Activate(newContent);
}
It either throws an exception that the region doesn't exist or an exception that creating the region failed and my ItemsControl.ItemsSource is already set. I didn't really expect this to work out of the box, but is there any way I can create dynamic regions and inject into them at run time?
Update: Calling RegisterViewWithRegion actually injects my textblock...but getting some weird behavior between tabs.
I changed it so I can choose the region and text I want to inject. It always works for the first region I create, but after that, flipping between tabs just shows the stuff I've added to the first region. Is the tab control re-using my datatemplate across multiple tabs? I've included all my code from the ViewModel. DynamicRegions is just an ObservableCollection