【问题标题】:How to add buttons on different tabs using TabControl in WPF如何在 WPF 中使用 TabControl 在不同的选项卡上添加按钮
【发布时间】:2010-02-11 15:37:31
【问题描述】:

我已经制作了基本选项卡,并且我希望每个选项卡都有一组不同的按钮,但是按钮被添加到主窗口并且没有选项卡。当我转到代码并剪切按钮并将其粘贴到我想要的选项卡时,它可以工作,但仅适用于 1 个按钮,然后对于第二个按钮,我收到一条错误消息:

“对象TabItem已经有一个孩子,不能添加Button.TabItem只能接受一个孩子。”

代码如下所示:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="394" Width="570">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*" />
        <ColumnDefinition Width="548*" />
    </Grid.ColumnDefinitions>
    <TabControl Margin="9,30,11,10" Name="tabControl1" Grid.Column="1" SelectionChanged="tabControl1_SelectionChanged">
        <TabItem Name="Admin" Header="Admin">

        </TabItem>
        <TabItem Name="Staff" Header="Staff" />
        <TabItem Name="Student" Header="Student" />
        <TabItem Name="Diary" Header="Diary" />
        <TabItem Name="Timetables" Header="Timetables" />
        <TabItem Name="Assignments" Header="Assignments" />
        <TabItem Name="Courses" Header="Courses" />
    </TabControl>
    <Menu Height="16" Name="menu1" VerticalAlignment="Top" Grid.Column="1" />
    <Button Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="18,64,0,0" Name="button1" VerticalAlignment="Top" Width="118">Add/Edit Student</Button>
</Grid>

我想在“管理”选项卡中实现“button1”

它可以工作,但是当我按第二个按钮时它会失败

提前致谢

【问题讨论】:

    标签: c# wpf button tabcontrol tabitem


    【解决方案1】:

    添加一个&lt;StackPanel&gt;,&lt;Grid&gt;, &lt;Border&gt;, 等--任何可以包含多个子元素的元素,就像你添加一个&lt;Window&gt; 这适用于任何只能接受一个子元素的 wpf 元素。按钮、切换按钮等

    <TabItem>
    <Grid>
    .......
    </Grid>
    </TabItem
    

    【讨论】:

      【解决方案2】:

      TabItem 继承自 ContentControl 类,它只允许 1 个对象作为其内容,因此您需要先添加一个允许多个对象的控件,例如 CanvasGrid,然后添加您的所有该对象的按钮。

      【讨论】:

        【解决方案3】:

        您收到该错误是因为 TabItem 只能包含一个 UIElement 作为子元素。您将需要使用某种面板(即 Grid、DockPanel、StackPanel、Canvas...)来安排您的所有 UIElement。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-12
          • 1970-01-01
          相关资源
          最近更新 更多