Dr.Kameleon
Dr.Kameleon

Reputation: 22820

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 :


<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?


P.S.

Upvotes: 0

Views: 69

Answers (1)

VidasV
VidasV

Reputation: 4895

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.

Upvotes: 2

Related Questions