动态添加 TabItem

发布于 2024-10-12 12:03:20 字数 1354 浏览 2 评论 0原文

我有一个 TabControl 控件

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

在后面的代码中,我将一些选项卡动态添加到 TabControl 中,如下所示:

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);
}

其中 VillageUserControlUserControl< /code> 处理指定的村庄。这段代码工作正常,并且得到了预期的结果...

问题是我不希望它出现在后面的代码中,而只是出现在 xaml 本身中。

我尝试这样做:

<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>

运行它后,它抛出一个异常:“指定的元素已经是另一个元素的逻辑子元素。首先断开它。”

我错过了什么吗?请在这里帮助我...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天暗了我发光 2024-10-19 12:03:20

你设置了错误的东西,你不应该修改 ItemContainerStyleTabControl.ItemTemplate 作为标题,TabControl.ContentTemplate 内容。

(例外情况可能与以下事实有关:在样式中仅创建了一个 VillageUserControl,但该样式适用于多个选项卡项目。)

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.)

叹梦 2024-10-19 12:03:20

现在它正在工作:

<TabControl Name="Farms_myVillages" 
           ItemsSource="{Binding Villages}">
       <TabControl.ItemTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
       </TabControl.ItemTemplate>
      <TabControl.ContentTemplate>
            <DataTemplate>
                <u:VillageResources/>
            </DataTemplate>
      </TabControl.ContentTemplate>
</TabControl>

Now it is working:

<TabControl Name="Farms_myVillages" 
           ItemsSource="{Binding Villages}">
       <TabControl.ItemTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
       </TabControl.ItemTemplate>
      <TabControl.ContentTemplate>
            <DataTemplate>
                <u:VillageResources/>
            </DataTemplate>
      </TabControl.ContentTemplate>
</TabControl>
衣神在巴黎 2024-10-19 12:03:20

您在代码后面不使用此方法的方法是正确的,而不是使用 ItemContainerStyle 使用 ItemTemplateContentTemplate。您可以查看 Josh Smith 提供的使用模板和样式创建选项卡的示例 -

http://code.msdn.microsoft.com/mag200902MVVM/Release/ProjectReleases.aspx?ReleaseId=2026

Your approach of not having this in code behind is right, instead of using ItemContainerStyle use ItemTemplate and ContentTemplate. You can have a look at this sample from Josh Smith for creating a tabs using Templates and Styles -

http://code.msdn.microsoft.com/mag200902MVVM/Release/ProjectReleases.aspx?ReleaseId=2026

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文