设置Grid.Row自动堆叠两行?

时间:2022-12-01 11:58:34

for the second row of the inner Grid I should set its Height explicitly, and if I set it to Auto the second and third rows are stacked over each other (will fill the same row even if the have different Grid.Row values):

对于内部网格的第二行,我应该明确地设置其高度,如果我将其设置为“自动”,则第二行和第三行相互堆叠(即使具有不同的Grid.Row值,也将填充相同的行):

<Window x:Class="Notifications.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Notifications"
        mc:Ignorable="d"
        Title="Fun with Notifications!" Height="225" Width="325" WindowStartupLocation="CenterOwner">
    <Grid IsSharedSizeScope="True"  Margin="5,0,5,5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="CarLabels"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Label Grid.Column="0" Content="Vehicle"/>
            <ComboBox Name="cboCars" Grid.Column="1" DisplayMemberPath="PetName" />
        </Grid>
        <Grid Grid.Row="1" DataContext="{Binding ElementName=cboCars, Path=SelectedItem}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="CarLabels"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="30"/> <!--If set to auto, it will stack over the next row-->
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Grid.Column="0" Grid.Row="0" Content="Id"/>
            <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=CarId}" />

            <Label Grid.Column="0" Grid.Row="1" Content="Make" Grid.RowSpan="2"/>
            <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Make}" Grid.RowSpan="2" />

            <Label Grid.Column="0" Grid.Row="2" Content="Color"/>
            <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Path=Color, UpdateSourceTrigger=PropertyChanged}"/>

            <Label Grid.Column="0" Grid.Row="3" Content="Pet Name"/>
            <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding Path=PetName}"/>

            <StackPanel Grid.Column="1" Grid.Row="4" HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,5,0,5">
                <Button x:Name="btnAddCar" Content="Add Car" Margin="5,0,5,0" Padding="4, 2" Click="btnAddCar_Click"/>
                <Button x:Name="btnChangeColor" Content="Change Color" Margin="5,0,5,0" Padding="4, 2" Click="btnChangeColor_Click"/>
                <Button x:Name="btnRemoveCar" Content="Remove Car" Margin="5,0,5,0" Padding="4,2" Click="btnRemoveCar_Click"/>
            </StackPanel>
            <Label Grid.Column="0" Grid.Row="5" Content="Is Changed"/>
            <CheckBox Grid.Column="1" Grid.Row="5" VerticalAlignment="Center" Margin="10,5,0,5" IsEnabled="False" IsChecked="{Binding Path=IsChanged}" />
        </Grid>
    </Grid>
</Window>

http://i.imgur.com/hcbVBb5.png

1 个解决方案

#1


0  

Remove Grid.RowSpan="2" for the Make Label and Textbox. This causes it to run onto the following row.

为Make Label和Textbox删除Grid.RowSpan =“2”。这会导致它运行到下一行。

It should be like this:

它应该是这样的:

    <Label Grid.Column="0" Grid.Row="1" Content="Make"/>
    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Make}"/>

#1


0  

Remove Grid.RowSpan="2" for the Make Label and Textbox. This causes it to run onto the following row.

为Make Label和Textbox删除Grid.RowSpan =“2”。这会导致它运行到下一行。

It should be like this:

它应该是这样的:

    <Label Grid.Column="0" Grid.Row="1" Content="Make"/>
    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Make}"/>