TabControl with Region

Mar 22, 2010 at 1:46 PM

Hi List,

my MainRegion is an ItemsControl. To this MainRegion i add different UserControls. One of this Controls is a TabControl with one fixed TabItem, the other two TabItems should be defiend as Regions, so i can change the contend of both by Commands.

Is there any chance to do this.

Thanks

Peter

 

Mar 22, 2010 at 7:00 PM

Hi Peter,

I can think of two possible approaches to achieve what you want. One of them is the one you are proposing, where your TabControl would look like this in XAML (the code below assumes you are using Silverlight, but for WPF it should be quite similar):

<Controls:TabControl>
    <Controls:TabItem>
        <TextBlock>I am in no region</TextBlock>
    </Controls:TabItem>
    <Controls:TabItem Regions:RegionManager.RegionName="RegionOne" Header="RegionOne"/>
    <Controls:TabItem Regions:RegionManager.RegionName="RegionTwo" Header="RegionTwo"/>
</Controls:TabControl>
As TabItems inherit from ContentControl, the ContentControlRegionAdapter will create a SingleActiveRegion for each of them.

Another possible approach could be marking the TabControl as a Region and add different views to it (still having a fixed TabItem). This approach can bee seen in the UIComposition Quickstarts (ViewDiscovery and View Injection).

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Mar 23, 2010 at 7:50 AM

Hi Damian,

solution two is exactly what i'm looking for.

Thanks

Peter

 

Mar 24, 2010 at 9:15 AM

Hi,

me agian.

I try your first approach.

For testing reasons i add one Label to "RegionOne" and "RegionTwo"

At runtime i got the following error:

ContentControl's Content 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 Content property,
    this exception may be caused by a change in the value of the inherited RegionManager attached property.

If both reagions are empty, there is no error

Does that means that i have to add my UserControls at runtime ?

Thanks

Peter

 

Apr 7, 2010 at 6:58 PM
Edited Apr 7, 2010 at 6:58 PM

Hi Peter,

In the case of the ContentControl (opposed to Selector and ItemsControl), you cannot make the control a Region if it already has content. So, if I understand correctly you added a Label inside each of the TabItems. As they are ContentControls you get an error. Summarizing, if you mark a ContentControl as a region you can only add views to it, not direct content.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Apr 8, 2010 at 10:57 AM

Hi ,

<if you mark a ContentControl as a region you can only add views to it, not direct content.>

that's what i did.

I mark my TabItems as regions as you suggested and the stuff is working very well.

Thanks a lot

Peter

 

Jul 9, 2010 at 2:22 PM

Hi Damian,

This works well in Xaml :

<Controls:TabControl>
    <Controls:TabItem>
        <TextBlock>I am in no region</TextBlock>
    </Controls:TabItem>
    <Controls:TabItem Regions:RegionManager.RegionName="RegionOne" Header="RegionOne"/>
    <Controls:TabItem Regions:RegionManager.RegionName="RegionTwo" Header="RegionTwo"/>
</Controls:TabControl>

But how can i do it in C# code behind if i want to dynamically add TabItem with new RegionName for each new TabItem?

it will help me a lot;)

thanks for your answer

Vincent

Oct 5, 2010 at 4:14 PM

Hi,

If your scenario requires to add multiple tabitems, you could use the Damian’s second approach. It allows you to add programmatically TabItems. For more information on this topic, you could check the View Injection Quickstart.

This Quickstart contains a TabControl marked as a region and has two fixed TabItems in xaml (General and Location). When an employee is selected the application programmatically adds a new TabItem (Current Projects) using View Injection.

Fernando Antivero
http://blogs.southworks.net/fantivero