TabControl 的 TabItem 共享相同的内容...不想要
以下示例 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑有一种更好的方法来实现你想要实现的目标,但我认为这会起作用(会测试,但我在 linux atm 上):
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):
如果您定义一个包含选项卡内容的用户控件,它将起作用。我创建了以下用户控件:
然后我将窗口 xaml 修改为:
这可能不完全是您想要的,但至少每个选项卡的布局仅在一个位置定义,然后您可以为每个用户控件分配一个数据上下文显示每个选项卡的值。
It will work if you define a usercontrol that has the tab content in it. I created the following usercontrol:
Then I modified my window xaml to be this:
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.
我遇到了同样的问题,并发现这篇文章解释了为什么会发生这种情况以及如何解决它。这是链接,以防其他人遇到同样的问题:
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