Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical scrolling inside GridView group of items in WinRT XAML

I am using GridView for displaying groups of different sets of items in a WinRT XAML app. Everything works well, except that the ItemsPanelTemplate uses a wrapping grid which stacks my items vertically when it's out of space.

So, I tried to use StackPanel, like this:

<GroupStyle.Panel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical" Visibility="Visible" />
    </ItemsPanelTemplate>
</GroupStyle.Panel>

The items are stacked vertically, and that's great, but the problem is now that I can't scroll them, and they don't fit on the screen. So I tried enabling vertical scrolling:

<GroupStyle.Panel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical" Visibility="Visible" 
                    ScrollViewer.VerticalScrollBarVisibility="Visible"
                    ScrollViewer.VerticalScrollMode="Enabled"/>
    </ItemsPanelTemplate>
</GroupStyle.Panel>

But that doesn't work. Any suggestions how to accomplish vertical scrolling inside GridView group?

EDIT 1:

I have also tried this:

<GroupStyle.Panel>
    <ItemsPanelTemplate>
        <ScrollViewer VerticalScrollBarVisibility="Visible"
                      HorizontalScrollMode="Disabled" 
                      ZoomMode="Disabled" 
                      VerticalScrollMode="Enabled">
              <StackPanel Orientation="Vertical" Visibility="Visible" />
         </ScrollViewer>
     </ItemsPanelTemplate>
 </GroupStyle.Panel>

This breaks the debugger as the ItemsPanelTemplate needs a panel as a child.

like image 343
Igor Ralic Avatar asked Jun 21 '12 09:06

Igor Ralic


2 Answers

OK, I finally solved it! To whom it may concern:

<GroupStyle.ContainerStyle>
    <Style TargetType="GroupItem">
        <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GroupItem">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <ContentPresenter Content="{TemplateBinding Content}" Grid.Row="0"/>
                    <ItemsControl x:Name="ItemsControl2" ItemsSource="{Binding GroupItems}" Grid.Row="1">
                        <ItemsControl.Template>
                        <ControlTemplate>
                            <ScrollViewer x:Name="ScrollViewer" VerticalScrollBarVisibility="Hidden"                                                  VerticalScrollMode="Enabled" HorizontalScrollBarVisibility="Disabled"                                                  HorizontalScrollMode="Disabled">
                            <ItemsPresenter />
                            </ScrollViewer>
                        </ControlTemplate>
                        </ItemsControl.Template>
                    </ItemsControl>
                </Grid>
           </ControlTemplate>
       </Setter.Value>
       </Setter>
   </Style>
</GroupStyle.ContainerStyle>

It's important that you use the Grid to make sure that the ScrollViewer scales correctly.

like image 169
Igor Ralic Avatar answered Nov 13 '22 01:11

Igor Ralic


What about this?

It renders elements like this:
Item 1 Item 2
Item 3 Item 4

<ListView Width="200">
    <ListBoxItem>
        <TextBlock>Item 1</TextBlock>
    </ListBoxItem>
    <ListBoxItem>
        <TextBlock>Item 2</TextBlock>
    </ListBoxItem>
    <ListBoxItem>
        <TextBlock>Item 3</TextBlock>
    </ListBoxItem>
    <ListBoxItem>
        <TextBlock>Item 4</TextBlock>
    </ListBoxItem>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapGrid Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
like image 24
Luis Cantero Avatar answered Nov 13 '22 01:11

Luis Cantero