如何在TabControl中的TabItem中移动窗口控件?

时间:2015-11-03 20:23:59

标签: wpf xaml layout grid tabcontrol

我在WPF应用程序中向我的窗口添加了一个TabControl,但我不确定如何重新定位TabControl以便所有其他控件(按钮,文本框,标签,数据网格)在“常规”选项卡项内。

我尝试将所有控件放在Tab General标题的TabItem元素中,但是我遇到了一系列错误:http://hastebin.com/isenokidev.xml

有谁知道如何在XAML中定义它?

这是窗口的xaml定义,其中包含TabControl之外的所有控件。我想知道如何在常规选项卡中放置它们(在相同的布局中):

<Window x:Class="MongoDBApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="800"
        Height="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="70" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="1*" />
            <RowDefinition Height=".50*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1.25*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width=".50*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width=".75*" />
        </Grid.ColumnDefinitions>
        <TabControl>
            <TabItem Header="General">


            </TabItem> 

            <TabItem Header="Security" />
            <TabItem Header="Details" />
        </TabControl>
        <DataGrid Name="infogrid"
                  Grid.Row="0"
                  Grid.RowSpan="3"
                  Grid.Column="3"
                  Grid.ColumnSpan="4"
                  Width="356"
                  HorizontalAlignment="Left"
                  AutoGenerateColumns="True" />
        <Label Grid.Row="4"
               Grid.Column="3"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Content="First Name:" />
        <TextBox Grid.Row="4"
                 Grid.Column="4"
                 Grid.ColumnSpan="2"
                 Width="120"
                 Height="23"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top"
                 Text=""
                 TextWrapping="Wrap" />
        <Label Grid.Row="5"
               Grid.Column="3"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Content="Last Name:" />
        <TextBox Grid.Row="5"
                 Grid.Column="4"
                 Grid.ColumnSpan="2"
                 Width="120"
                 Height="23"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top"
                 Text=""
                 TextWrapping="Wrap" />
        <Label Grid.Row="6"
               Grid.Column="3"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Content="Department" />
        <TextBox Grid.Row="6"
                 Grid.Column="4"
                 Grid.ColumnSpan="2"
                 Width="120"
                 Height="23"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top"
                 Text=""
                 TextWrapping="Wrap" />


        <Button x:Name="saveBtn"
                Grid.Row="7"
                Grid.Column="3"
                Width="75"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Content="Save" />
        <Button x:Name="updateBtn"
                Grid.Row="7"
                Grid.Column="4"
                Width="75"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Content="Update" />
        <Button x:Name="deleteBtn"
                Grid.Row="7"
                Grid.Column="5"
                Width="75"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Content="Delete" />
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:2)

要使所有控件都位于“常规”标签项中,需要在 中定义它们。我不确定你为什么早些时候收到错误,但这很好用:

<Grid>
    <TabControl>
        <TabItem Header="General">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="70" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height=".50*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width=".75*" />
                </Grid.ColumnDefinitions>

                <DataGrid Name="infogrid"
                          Grid.Row="0"
                          Grid.RowSpan="3"
                          Grid.ColumnSpan="4"
                          Width="356"
                          HorizontalAlignment="Left"
                          AutoGenerateColumns="True" />

                <Label Grid.Row="4"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Top"
                       Content="First Name:" />
                <TextBox Grid.Row="4"
                         Grid.Column="1"
                         Grid.ColumnSpan="2"
                         Width="120"
                         Height="23"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Top"
                         Text=""
                         TextWrapping="Wrap" />

                <Label Grid.Row="5"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Top"
                       Content="Last Name:" />
                <TextBox Grid.Row="5"
                         Grid.Column="1"
                         Grid.ColumnSpan="2"
                         Width="120"
                         Height="23"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Top"
                         Text=""
                         TextWrapping="Wrap" />

                <Label Grid.Row="6"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Top"
                       Content="Department" />
                <TextBox Grid.Row="6"
                         Grid.Column="1"
                         Grid.ColumnSpan="2"
                         Width="120"
                         Height="23"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Top"
                         Text=""
                         TextWrapping="Wrap" />

                <Button x:Name="saveBtn"
                        Grid.Row="7"
                        Width="75"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Content="Save" />

                <Button x:Name="updateBtn"
                        Grid.Row="7"
                        Grid.Column="1"
                        Width="75"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Content="Update" />

                <Button x:Name="deleteBtn"
                        Grid.Row="7"
                        Grid.Column="2"
                        Width="75"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Content="Delete" />
            </Grid>
        </TabItem>

        <TabItem Header="Security" />

        <TabItem Header="Details" />
    </TabControl>
</Grid>