TabControl 的 TabItems 共享相同的内容......不想要 [英] TabControl's TabItems sharing same content... Do Not Want

查看:20
本文介绍了TabControl 的 TabItems 共享相同的内容......不想要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

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

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?

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

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.

编辑

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

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.

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

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.

推荐答案

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

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>

这篇关于TabControl 的 TabItems 共享相同的内容......不想要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆