WPF - TabControl 绑定到 ObservableCollection 中的 TabItem 属性
我有一个 ObservableCollectionItemsSource
属性。 Item
类包含一个返回 System.Windows.Controls.TabItem
的属性 TabItem
。我希望 TabControl 显示集合中的 TabItem
。
(实际上,“Item”类上有很多属性。)
代码:
Item 类:
public class Item
{
public Item(TabItem tabItem)
{
this.TabItem = tabItem;
}
public TabItem TabItem { get; private set; }
}
TabControl XAML:
<TabControl x:Name="tabControl" />
代码隐藏:
this.tabControl.ItemsSource = new ObservableCollection<Item>()
{
new Item(new TabItem() {Header = "TabItem 1 Header", Content = "TabItem 1 Content"}),
new Item(new TabItem() {Header = "TabItem 2 Header", Content = "TabItem 2 Content"}),
new Item(new TabItem() {Header = "TabItem 3 Header", Content = "TabItem 3 Content"}),
new Item(new TabItem() {Header = "TabItem 4 Header", Content = "TabItem 4 Content"}),
new Item(new TabItem() {Header = "TabItem 5 Header", Content = "TabItem 5 Content"}),
};
我尝试设置 TabControl
的 DisplayMemberPath 到“TabItem”,但这不起作用。我无法让
ItemTemplate
和 ContentTemplate
实际显示 TabItem
(我可以分别绑定到 TabItems 的标题和内容,但这不是我想要的)。
如果我使用 ObservableCollection
并将其设置为 ItemsSource
,它会按您的预期显示 TabItem
,但我不能通过这个额外的步骤让它工作。
我做错了什么?有办法让它发挥作用吗?
I have an ObservableCollection<Item>
and I want to set it as the ItemsSource
property of a TabControl. The Item
class contains a Property TabItem
that returns a System.Windows.Controls.TabItem
. I want the TabControl to display the TabItem
s from the collection.
(In reality, there are lots of properties on the "Item" class.)
Code:
Item class:
public class Item
{
public Item(TabItem tabItem)
{
this.TabItem = tabItem;
}
public TabItem TabItem { get; private set; }
}
TabControl XAML:
<TabControl x:Name="tabControl" />
Code behind:
this.tabControl.ItemsSource = new ObservableCollection<Item>()
{
new Item(new TabItem() {Header = "TabItem 1 Header", Content = "TabItem 1 Content"}),
new Item(new TabItem() {Header = "TabItem 2 Header", Content = "TabItem 2 Content"}),
new Item(new TabItem() {Header = "TabItem 3 Header", Content = "TabItem 3 Content"}),
new Item(new TabItem() {Header = "TabItem 4 Header", Content = "TabItem 4 Content"}),
new Item(new TabItem() {Header = "TabItem 5 Header", Content = "TabItem 5 Content"}),
};
I've tried setting the TabControl
's DisplayMemberPath
to "TabItem" but that didn't work. I've been unable to get ItemTemplate
and ContentTemplate
to actually display the TabItem
(I could bind to the Header and Content of the TabItems respectively, but that's not what I want).
If I were using a ObservableCollection<TabItem>
and set it to ItemsSource
it displays the TabItem
s as you would expect, yet I can't get it to work with this additional step.
What am I doing wrong? Is there a way to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为如果您摆脱 Item 类并仅添加 TabItems,您的代码就可以工作。但是,通过这样做,您可以限制 TabItem 样式的功能。
I think if you got rid of the Item class and just added TabItems, your code would work. By doing it that way however, your limiting what you can do with the styling of the TabItems.
我认为你应该使用样式来设置内容。您有 ItemContainerStyle 这将有所帮助,如下所述:
WPF TabControl 数据绑定
:)
I think you should use style to set the content. You have ItemContainerStyle which would help as stated here :
WPF TabControl Databinding
:)