Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3760480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:28:41+00:00 2026-05-19T10:28:41+00:00

I have a TabControl control <TabControl Name=Farms_myVillages ItemsSource={Binding Villages}> </TabControl/> In the code behind

  • 0

I have a TabControl control

<TabControl Name="Farms_myVillages"
            ItemsSource="{Binding Villages}">
</TabControl/>

In the code behind I add some tabs dynamically to the TabControl as follows:

foreach (Village vill in Villages)
{
    TabItem tab = new TabItem();
    tab.Header = vill.Name;
    VillageUserControl c = new VillageUserControl();
    c.DataContext = vill;
    tab.Content = c;
    Farms_myVillages.Items.Add(tab);
}

where VillageUserControl is a UserControl that deal with the specified village. This code works fine and it gets the expected results…

The problem is that I don’t want this to be in the code behind but just in the xaml itself.

I try this:

<TabControl Name="Farms_myVillages"
            ItemsSource="{Binding Villages}">
      <TabControl.ItemContainerStyle>
          <Style TargetType="TabItem">
              <Setter Property="Header" Value="{Binding Name}"/>
              <Setter Property="Content">
                 <Setter.Value>
                    <u:VillageUserControl DataContext="{Binding}"/>
                 </Setter.Value>
              </Setter>
          </Style>
      </TabControl.ItemContainerStyle>
</TabControl>

After I run it, it throws an exception: “Specified element is already the logical child of another element. Disconnect it first.”

Did I miss something? Please help me here…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:28:41+00:00Added an answer on May 19, 2026 at 10:28 am

    You set the wrong thing, you should not modify the ItemContainerStyle but the TabControl.ItemTemplate for the header and TabControl.ContentTemplate for the content.

    (The exception may have to do with the fact that in the style only one VillageUserControl is created, but the style applies to multiple tab items.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data bound tab control: <TabControl ItemsSource={Binding Products} Name=ProductsTabControl> <TabControl.ItemTemplate> <DataTemplate> <TextBlock
I have a tab control: <TabControl IsSynchronizedWithCurrentItem=True ItemsSource={Binding} ItemTemplate={StaticResource ClosableTabItemTemplate} > </TabControl> I want
I have a TabControl that can be rearranged by dragging/dropping the tabs. The current
I have a custom control with bindings like below <DataTemplate DataType={x:Type vm:EditorTabViewModel}> <me:MarkdownEditor Options={Binding
I have a TabControl that contains several tabs. Each tab has one UserControl on
I have a TabControl, and on the second tab is a button: <Button Name=deleteResourceBtn>
I have a WPF TabControl, with some TabItems. I want margins on the left
I have three tabs in a Windows Forms tab control. Currently in Tab1 I
I have a Tab Control with multiple Tab Pages. I want to be able
I have a page with a tab control and each control has almost 15

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.