WPF - TabControl 绑定到 ObservableCollection 中的 TabItem 属性

发布于 2024-09-28 01:28:31 字数 1530 浏览 2 评论 0原文

我有一个 ObservableCollection,我想将其设置为 TabControl 的 ItemsSource 属性。 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"}),
};

我尝试设置 TabControlDisplayMemberPath 到“TabItem”,但这不起作用。我无法让 ItemTemplateContentTemplate 实际显示 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 TabItems 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 TabItems 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 技术交流群。

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

发布评论

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

评论(2

暮年慕年 2024-10-05 01:28:31

我认为如果您摆脱 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.

软的没边 2024-10-05 01:28:31

我认为你应该使用样式来设置内容。您有 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

:)

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