网格行不是我设置的(相对)高度

时间:2016-02-04 14:38:28

标签: wpf xaml

当我遇到这个问题时,我正在查看View中的UserControl:

我将内容网格行高设置为2 *,1 *和1 *,两者之间的线设置为自动。
现在。最上面的行有一个嵌套在TextBlock和TextBox中的网格。细胞的高度绝不是2 *。如果我将它设置为自动,它看起来就好像。我很确定其他2个内容行(包含按钮)也是正确的高度。

现在我希望你们能帮助我解决这个问题。我用Google搜索了一下,似乎无法找到合适的解决方案。

以下是代码:

<Grid Margin="5">
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Name="TBCustomerTitle" Margin="0,0,8,0" FontWeight="Bold" 
               Visibility="{Binding SetVisibility, UpdateSourceTrigger=PropertyChanged}">Customer:</TextBlock>
        <TextBox Name="TBCustomerData" Grid.Column="1"  
             Visibility="{Binding SetVisibility, UpdateSourceTrigger=PropertyChanged}"
             Text="{Binding DisplayedCustomer.Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
    </Grid>

    <Line Grid.Row="1" Style="{StaticResource horizontalLineStyle}" />

    <StackPanel Grid.Row="2" Orientation="Vertical">                                 
        <Button Command="{Binding AddCommand}">Add a new customer</Button>
        <Button Command="{Binding UpdateCommand}">Update the selected customer</Button>
        <Button Command="{Binding DeleteCommand}">Delete the selected customer</Button>
    </StackPanel>

    <Line Grid.Row="3" Style="{StaticResource horizontalLineStyle}" />

    <StackPanel Grid.Row="4" Orientation="Vertical">
        <Button Command="{Binding RefreshCommand}">Refresh the list</Button>
        <Button Command="{Binding ClearSelectionCommand}">Clear selection</Button>
    </StackPanel>

</Grid>

编辑:现在有截图,按要求。这是我正在谈论的正确的专栏(所以,是的,这个UserControl本身就是另一个嵌套的网格)

Screenshot of current application

编辑2:以下是包含的代码。我的页面由三个classess组成 第一个(ApplicationView.xaml)是一个Window。它在左栏中有导航按钮,有一条很好的线,还有一列包含该特定对象的视图。它没有行。
ApplicationView.xaml

<Grid>
    <Grid.Resources>
        <ResourceDictionary Source="StylesRD.xaml" />
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="6*" />
    </Grid.ColumnDefinitions>

    <!-- A DockPanel for the Navigation Buttons-->
    <DockPanel Grid.Column="0" Margin="0,20" VerticalAlignment="Top" HorizontalAlignment="Center">
            <ItemsControl ItemsSource="{Binding PageViewModels}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>                            
                        <Button Content="{Binding Name}"
                                Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                                CommandParameter="{Binding }"
                                Margin="2,5"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
    </DockPanel>

    <Line Grid.Column="1" Style="{StaticResource verticalLineStyle}" />

    <ContentControl Grid.Column="2" Content="{Binding CurrentPageViewModel}" />
</Grid>

视图(CustomerView.xaml)是第二个文件。它是一个用户控件,它还有3列,其中一列是本例中的Customers列表,一列是美丽的分隔线(它很漂亮,对吧?/讽刺)和我现在正在努力争取的那一列细节和做东西的按钮 这里也没有行 的 CustomerView.xaml

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>

    <!-- Customer list -->
    <view:CustomerListView Grid.Column="0" VerticalAlignment="Top"/>

    <Line Grid.Column="1" Style="{StaticResource verticalLineStyle}" />

    <!-- Details -->
    <view:CustomerDetailsView Grid.Column="2" VerticalAlignment="Top"/>

</Grid>

2 个答案:

答案 0 :(得分:1)

您的内部Grid有两个RowDefinition没有高度设置。因此,默认Height="*"将用于两行。您的内部网格不需要两个RowDefinitions,因此删除它们应该可以解决您的问题。因此,您的内部网格定义应如下所示:

    <Grid>
        <!--
        remove these row definitions
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        -->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Name="TBCustomerTitle" Margin="0,0,8,0" FontWeight="Bold" 
               Visibility="{Binding SetVisibility, UpdateSourceTrigger=PropertyChanged}">Customer:</TextBlock>
        <TextBox Name="TBCustomerData" Grid.Column="1"  
             Visibility="{Binding SetVisibility, UpdateSourceTrigger=PropertyChanged}"
             Text="{Binding DisplayedCustomer.Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
    </Grid>

修改

它对我有用。 TextBoxTextBlock被拉伸至2*高度,两个StackPanels的高度均为1*。请提供您使用用户控件的XAML。似乎存在问题。

example

编辑2:

啊,现在我明白了。 VerticalAlignment="Top"中的CustomerView.xaml设置会将您的CustomerDetailsView推到顶端。删除此属性或将VerticalAlignment设置为Stretch

答案 1 :(得分:1)

您提供的xaml工作正常,问题出在包含xaml中。看看这个例子xaml。

<StackPanel Orientation="Horizontal">
    <Border Background="Yellow">
        <local:MyUserControl/>
    </Border>
    <StackPanel Background="Green">
        <local:MyUserControl/>
    </StackPanel>
</StackPanel>

用户控件(剥离绑定和样式)

<UserControl x:Class="WpfApplication3.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication3"
             mc:Ignorable="d">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <TextBlock Name="TBCustomerTitle" Margin="0,0,8,0" FontWeight="Bold" >Customer:</TextBlock>
            <TextBox Name="TBCustomerData" Grid.Column="1" Text="{Binding DisplayedCustomer.Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        </Grid>

        <Line Grid.Row="1" Height="5"/>

        <StackPanel Grid.Row="2" Orientation="Vertical">
            <Button Command="{Binding AddCommand}">Add a new customer</Button>
            <Button Command="{Binding UpdateCommand}">Update the selected customer</Button>
            <Button Command="{Binding DeleteCommand}">Delete the selected customer</Button>
        </StackPanel>

        <Line Grid.Row="3" Height="5"/>

        <StackPanel Grid.Row="4" Orientation="Vertical">
            <Button Command="{Binding RefreshCommand}">Refresh the list</Button>
            <Button Command="{Binding ClearSelectionCommand}">Clear selection</Button>
        </StackPanel>
    </Grid>
</UserControl>

正如您所看到的,堆栈面板中的用户控件没有拉伸,因为stackpanel不会拉伸它的内容。