Silverlight ItemsControl vertical scrollbar, using a wrappanel as ControlTemplate

10,113

It's working perfectly. i had two different Accordions on the same page, and I was checking my code changes in the one whose code I wasn't touching. Sometimes you need to pause, go for a walk and then look at the whole screen. The right code is the last one:

               <ItemsControl ItemsSource="{Binding StageVideos}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                                <controlsToolkit:WrapPanel />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Image Margin="2" Width="150" Cursor="Hand" MouseLeftButtonDown="videoPreview_MouseLeftButtonDown" Tag="{Binding}" Source="{Binding PreviewImage, Converter={StaticResource ImageConverter}}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.Template>
                        <ControlTemplate>
                            <ScrollViewer VerticalScrollBarVisibility="Visible">
                                <ItemsPresenter />
                            </ScrollViewer>
                            </ControlTemplate>
                    </ItemsControl.Template>
                </ItemsControl>
Share:
10,113
Orestes
Author by

Orestes

I'm a web/software developer based in Barcelona. I enjoy learning new programming languages and techniques :)

Updated on June 05, 2022

Comments

  • Orestes
    Orestes almost 2 years

    I have a collection of elements, each one with a name and a subcollection of image blobs. I want to display an Accordion, with each item representing each of the MainElements. inside each element, I display the images in the subcollecion of said MainElement. The Accordion gets resized by the user, so I use a wrappanel for presenting the images. When the accordion is wide enough, the images reorder themselves fitting as many as posible in each row. the problem comes when the wrappanel only displays one image per row (because there's no space enough for more), the image list continues, but I can't see all the images, because they don't fit inside the control's height. I need a vertical scrollbar to be displayed inside the AccordionItem so I can scroll down the image list. So, here's my code:

    <layoutToolkit:Accordion Width="Auto" Height="Auto" ItemsSource="{Binding MainElementCollection}">
        <layoutToolkit:Accordion.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding MainElementName}" />
            </DataTemplate>
        </layoutToolkit:Accordion.ItemTemplate>
        <layoutToolkit:Accordion.ContentTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding SubElementCollection}" ScrollViewer.VerticalScrollBarVisibility="Auto" >
                        <ItemsControl.Template>
                            <ControlTemplate>
                                <controlsToolkit:WrapPanel />
                            </ControlTemplate>
                        </ItemsControl.Template>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Image Margin="2" Width="150" Source="{Binding PreviewImage, Converter={StaticResource ImageConverter}}" />
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
            </DataTemplate>
        </layoutToolkit:Accordion.ContentTemplate>
    </layoutToolkit:Accordion>
    

    http://www.silverlightshow.net/tips/How-to-add-scrollbars-to-ItemsControl.aspx suggests that I should surround my wrappanel with a scrollviewer, like this

                    <ItemsControl.Template>
                        <ControlTemplate>
                            <scrollviewer>
                            <controlsToolkit:WrapPanel />
                            </scrollviewer>
                        </ControlTemplate>
                    </ItemsControl.Template>
    

    But then my wrappanel gets really small and I can only see a small vertical scrollbar Any ideas? Thanks a lot.

    Edit: I figured thatthe wrappanel loses its width when used in the controltemplate

    It should be used as follows:

                                   <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                                <controlsToolkit:WrapPanel ScrollViewer.VerticalScrollBarVisibility="Visible" />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
    

    Anyway, I tried adding the ScrollViewer.VerticalScrollBarVisibility="Visible" line but I'm stuck again.

    Edited again:

    Now my wrappanel looks like this:

                        <ItemsControl ItemsSource="{Binding StageVideos}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                        <controlsToolkit:WrapPanel />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Image Margin="2" Width="150" Cursor="Hand" MouseLeftButtonDown="videoPreview_MouseLeftButtonDown" Tag="{Binding}" Source="{Binding PreviewImage, Converter={StaticResource ImageConverter}}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.Template>
                                <ControlTemplate>
                                    <ScrollViewer VerticalScrollBarVisibility="Visible">
                                        <ItemsPresenter />
                                    </ScrollViewer>
                                    </ControlTemplate>
                            </ItemsControl.Template>
                        </ItemsControl>
    

    I'm using a wrappanel as the items panel, and I'm using the ControlTemplate to surround the presenter with a scrollviewer. Still, no luck :/