【问题标题】:WPF ListBox in TabItem "Property content set more than once"TabItem中的WPF ListBox“属性内容设置不止一次”
【发布时间】:2014-12-17 15:38:40
【问题描述】:

我(显然)是 WPF 的新手,并且正在努力弄清楚如何将 ListBox 放入 TabItem 中。我目前在下面的尝试导致Property 'Content' set more than once,所有我似乎与我无关的答案。

<Window>
<Grid>
    <TabItem>
        <Grid Background="Red"></Grid>
            <TabItem.Header>
                <DockPanel Width="130" HorizontalAlignment="Center">
                    <TextBlock Text="Inventory" Foreground="Black" FontSize="30" TextAlignment="Center"/>
                </DockPanel>
            </TabItem.Header>
        <ListBox>
            <ListBoxItem>Test</ListBoxItem>
        </ListBox>
   </TabItem>
</Grid>
</Window>

如果有人能指出我正确的方向,我将不胜感激。

【问题讨论】:

  • &lt;Grid Background="Red"&gt;&lt;/Grid&gt;有什么用?取下来看看。那应该可以。

标签: wpf xaml listbox


【解决方案1】:

好的,实际有效的编辑答案如下。事实上,正如其他人所建议的那样,删除 "&lt;Grid Background="Red"&gt;&lt;/Grid&gt;" 行可以解决问题。删除以下行也是如此:

<ListBox>
    <ListBoxItem>Test</ListBoxItem>
</ListBox>

Grid 和 ListBox 视图都被设置为 TabItem 的 Content 属性,这就是错误的原因。

如果我们真的希望将两个 ListBox 都用作 TabItem 的内容,我们需要将它们都放在一个堆栈面板中,如下所示:

<Window>
    <Grid>
        <TabItem>
            <TabItem.Header>
                <DockPanel Width="130" HorizontalAlignment="Center">
                    <TextBlock Text="Inventory" Foreground="Black" FontSize="30" TextAlignment="Center"/>
                </DockPanel>
            </TabItem.Header>
            <StackPanel Orientation="Horizontal">
                <Grid Background="Red"></Grid>

                <ListBox>
                    <ListBoxItem>Test</ListBoxItem>
                </ListBox>
            </StackPanel>
        </TabItem>
    </Grid>
</Window>

【讨论】:

  • IMO &lt;Grid Background="Red"&gt;&lt;/Grid&gt; 没用,因此可以完全删除。 +1 虽然..
  • 是的,可能是这种情况,但仍然需要 OP 正确理解错误:)
  • 不能放在 中。它需要是 TabItem 的一级后代。删除 将解决问题。
  • 编辑了我的答案,感谢您的 cmets。现在应该没问题了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 2012-11-13
  • 1970-01-01
相关资源
最近更新 更多