动态添加 TabItem
我有一个 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);
}
其中 VillageUserControl
是 UserControl< /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你设置了错误的东西,你不应该修改
ItemContainerStyle
但TabControl.ItemTemplate
作为标题,TabControl.ContentTemplate
内容。(例外情况可能与以下事实有关:在样式中仅创建了一个
VillageUserControl
,但该样式适用于多个选项卡项目。)You set the wrong thing, you should not modify the
ItemContainerStyle
but theTabControl.ItemTemplate
for the header andTabControl.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.)现在它正在工作:
Now it is working:
您在代码后面不使用此方法的方法是正确的,而不是使用
ItemContainerStyle
使用ItemTemplate
和ContentTemplate
。您可以查看 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
useItemTemplate
andContentTemplate
. 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