网格中的StackPanel:限制高度

时间:2013-08-12 10:49:58

标签: c# xaml layout winrt-xaml

我有一个包含多个行和列的大型主网格。我想在这些单元格中放置一个垂直的stackpanel。在这个堆栈面板中有一个文本块和一个滚动查看器。我的问题是,stackpanel不受单元格的限制,而是stackpanel变得足够大以适应整个scrollviewer。

我该如何解决这个问题?

编辑:我的xaml代码:

<Grid x:Name="Grid1" Margin="120,0,0,0" Width="1244">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0*"/>
                        <ColumnDefinition Width="33*"/>
                        <ColumnDefinition Width="40"/>
                        <ColumnDefinition Width="50*"/>
                        <ColumnDefinition Width="40"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="71"/>
                        <RowDefinition Height="40"/>
                        <RowDefinition/>
                        <RowDefinition Height="20"/>
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Column="3" Grid.Row="2" Grid.ColumnSpan="2" Margin="0">
                        <TextBlock TextWrapping="Wrap" FontSize="48" Margin="0" VerticalAlignment="Top" Foreground="#FF0083FF" Text="Top 10:" HorizontalAlignment="Left" FontFamily="Segoe UI Light"/>
                        <ScrollViewer Margin="0,20,0,0" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Visible">
                            <StackPanel>
                                <ListView x:Name="TopListView" ItemsSource="{Binding}" SelectionMode="None" Foreground="White" >
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <StackPanel >
                                                    <TextBlock FontSize="32" Text="1" Foreground="#FF0083FF"/>
                                                </StackPanel>
                                                <TextBlock Text="{Binding Text}" Foreground="Black" 
                                                    FontSize="16" Margin="0,0,0,0" TextWrapping="Wrap" />

                                            </StackPanel>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>
                            </StackPanel>
                        </ScrollViewer>
                    </StackPanel>

                </Grid>

1 个答案:

答案 0 :(得分:3)

使用另一个Grid而不是StackPanel:

<Grid Grid.Column="3" Grid.Row="2" Grid.ColumnSpan="2" Margin="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock FontSize="48" FontFamily="Segoe UI Light"
               Foreground="#FF0083FF" HorizontalAlignment="Left"
               TextWrapping="Wrap" Text="Top 10:"/>
    <ScrollViewer Grid.Row="1" Margin="0,20,0,0"
                  HorizontalScrollBarVisibility="Disabled"
                  VerticalScrollBarVisibility="Visible">
        ...
    </ScrollViewer>
</Grid >