TabControl 的 TabItem 共享相同的内容...不想要

发布于 2024-09-14 20:41:50 字数 1043 浏览 5 评论 0原文

以下示例 xaml 使每个选项卡项共享相同的 TextBox。我想,在某种程度上,这是有道理的……但这是意想不到的行为,几乎感觉像是一个错误。我在文档中找不到任何解释该行为或如何正确解决该行为的信息。

<TabControl>
    <TabControl.Resources>
        <Style TargetType="TabItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBox />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>
    <TabItem Name="tab1" />
    <TabItem Name="tab2" />
</TabControl>

在 tab1 和 tab2 之间切换时,使用相同的 TextBox,而我希望每个选项卡都有一个新的 TextBox。我怎样才能得到后一种情况?

子类化 TabItem 并默认将其内容设置为 TextBox 是一种方法,但我只是想确保没有遗漏的内容。

编辑

我意识到为每个选项卡显式设置内容可以解决问题,但选项卡是动态创建的。我想使用内容模板,以便我可以通过数据绑定添加新选项卡,并取消共享内容,因为它会导致奇怪的行为。

也许在 TabControl 的当前实现中,没有声明性方法来解决这个问题。在代码中设置内容非常简单,但在 WPF 中这样的事情总是感觉不对。对我来说,这似乎是对 TabControl 的不合理优化;对于不切实际的情况,它至少应该是可选的。

The following example xaml causes each tab item to share the same TextBox. It makes sense, on some level I guess... but it's unexpected behavior, and almost feels like a bug. And I couldn't find any information in the docs explaining the behavior, or how to correctly get around it.

<TabControl>
    <TabControl.Resources>
        <Style TargetType="TabItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBox />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>
    <TabItem Name="tab1" />
    <TabItem Name="tab2" />
</TabControl>

When switching between tab1 and tab2, the same TextBox is used, when I would expect a new TextBox for each tab. How can I get the latter case?

Subclassing TabItem and making its content a TextBox by default is one way to do it, but I just want to be sure there isn't something I'm missing.

Edit

I realize that setting the content explicitly for each tab will solve the problem, but the tabs are to be created dynamically. I want to use the content template so that I may add new tabs via data binding and have the content unshared as it is causing quirky behavior.

Perhaps with the current implementation of the TabControl, there is no declarative approach to solving this. It's pretty trivial setting the content in code, but such things always feel wrong in WPF. To me, this seems like an unjustified optimization of the TabControl; it should at least be optional for situations where it is not practical.

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

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

发布评论

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

评论(3

瀟灑尐姊 2024-09-21 20:41:50

我怀疑有一种更好的方法来实现你想要实现的目标,但我认为这会起作用(会测试,但我在 linux atm 上):

<TabControl>
    <TabControl.Resources>
        <Style TargetType="TabItem" x:Shared="False">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBox />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>
    <TabItem Name="tab1" />
    <TabItem Name="tab2" />
</TabControl>

I suspect there's a nicer way to achieve whatever it is you're trying to achieve, but I think this will work (would test but I'm on linux atm):

<TabControl>
    <TabControl.Resources>
        <Style TargetType="TabItem" x:Shared="False">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBox />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>
    <TabItem Name="tab1" />
    <TabItem Name="tab2" />
</TabControl>
爱你是孤单的心事 2024-09-21 20:41:50

如果您定义一个包含选项卡内容的用户控件,它将起作用。我创建了以下用户控件:

<UserControl x:Class="SO_Testing.TextBoxUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBox/>
    </Grid>
</UserControl>

然后我将窗口 xaml 修改为:

<Window x:Class="SO_Testing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SO_Testing"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TabControl>
            <TabItem Name="tab1" Header="Test">
                <local:TextBoxUC/>
            </TabItem>
            <TabItem Name="tab2" Header="Test 2">
                <local:TextBoxUC/>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

这可能不完全是您想要的,但至少每个选项卡的布局仅在一个位置定义,然后您可以为每个用户控件分配一个数据上下文显示每个选项卡的值。

It will work if you define a usercontrol that has the tab content in it. I created the following usercontrol:

<UserControl x:Class="SO_Testing.TextBoxUC"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBox/>
    </Grid>
</UserControl>

Then I modified my window xaml to be this:

<Window x:Class="SO_Testing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SO_Testing"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TabControl>
            <TabItem Name="tab1" Header="Test">
                <local:TextBoxUC/>
            </TabItem>
            <TabItem Name="tab2" Header="Test 2">
                <local:TextBoxUC/>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

This may not be exactly what you are wanting, but at least the layout of each tab is only defined in one place and you can then assign a datacontext to each usercontrol to show the values for each of your tabs.

冰雪梦之恋 2024-09-21 20:41:50

我遇到了同样的问题,并发现这篇文章解释了为什么会发生这种情况以及如何解决它。这是链接,以防其他人遇到同样的问题:

http://www.codeproject.com/Articles/460989/WPF-TabControl-Turning-Off-Tab-Virtualization

I had the same problem, and found this post explaining why this happens and how to workaround it. This is the link, just in case someone else come around with the same issue:

http://www.codeproject.com/Articles/460989/WPF-TabControl-Turning-Off-Tab-Virtualization

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