简体   繁体   中英

Trouble with Grid and StackPanel - Bindings

OK, I'm having issues properly using a ScrollViewer (containing an ItemsControl ) inside a StackPanel .

The thing is the scrolling works, however :

  • No matter how much the user scrolls down, the bottom lines of the tables are not visible
  • When scrolling is... "released", the table pops back up ...

<ScrollViewer Height="Auto">
    <ItemsControl ItemsSource="{Binding Items, Mode=TwoWay}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid VerticalAlignment="Top" Margin="0,0,0,0" x:Name="ResTable">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.10*"/>
                        <ColumnDefinition Width="0.30*" />
                        <ColumnDefinition Width="0.30*"/>
                        <ColumnDefinition Width="0.30*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding [num]}" HorizontalAlignment="Center" />
                    <TextBlock Grid.Column="1" Text="{Binding [A]}" HorizontalAlignment="Center" />
                    <TextBlock Grid.Column="2" Text="{Binding [B]}" HorizontalAlignment="Center" />
                    <TextBlock Grid.Column="3" Text="{Binding [C]}" HorizontalAlignment="Center" />
                </Grid>
            </DataTemplate>

        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

Any ideas?


PS

  • I've had a look at some of the other answers to similar issues, but none seems to be working...

  • My main idea is to display a table, with a fixed header , and scrollable contents (populated via Bindings) - ok, and there are also a couple of things on the top of the page (apart from the table I mean)

I guess the issue is that you should probably use parent element like Grid , not StackPanel , because StackPanel has its drawbacks when resizing child items with scrolls and so on.

<Grid>
<Grid.RowDefinitions>
   <RowDefinition Height="Auto"/>
   <RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!-- Put headers in Grid.Row="0" -->

<!-- Put Scrollviewer in Grid.Row="1" --> 

</Grid>

Also Height="Auto" attribute can be removed from ScrollViewer , you might want to use VerticalAlignment="Stretch" for item to take all the available space. I hope this is what you are trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM