Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Scrollbar in ListBox not working! GRRR! RSS

2 replies

Last post Sep 22, 2008 01:51 PM by rmcsharry

(0)
  • rmcsharry

    rmcsharry

    Member

    380 Points

    277 Posts

    Scrollbar in ListBox not working! GRRR!

    Sep 19, 2008 06:40 PM | LINK

    Hi,

    This is driving me nuts.

    With the following code, the listbox does not dispaly a scrollbar. If I add the verticalscrollbar by setting it to visible, it shows but you cannot use it to scroll. If I remove the ItemsPanelTemplate (ie.the wrap panel) then the vertical scrollbar appears and I can scroll the list of stackpanels. But I need the wrap-panel in there, as I need more than one item on each listbox row.

    This almost identical code works in WPF (I even found the same example in Pro WPF in C-Sharp 2008).

    Does anyone know why this does not work?

    <Grid x:Name="LayoutRoot" Width="400" Height="300" Background="DarkGray">
           
         <ListBox x:Name="myList" Margin="5,5,5,5">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <local:WrapPanel>

                        </local:WrapPanel>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>

                <ListBox.ItemTemplate>
                <DataTemplate>

                    <StackPanel Orientation="Vertical" Margin="5" Width="110" Height="150">                      
                        <TextBlock Text="{Binding PictureID}"/>
                        <Image Width="100" Height="100" Source="{Binding Picture1, Converter={StaticResource ImageConverter}}"/>
                        <TextBlock Text="{Binding PictureName}"/>                    
                    </StackPanel>

                </DataTemplate>         
              </ListBox.ItemTemplate>      
               
            </ListBox>
        </Grid>

    The weird thing is that I've tried using a grid instead with my own ScrollViewer, and although that ScrollViewer appears, you can only scroll it a tiny bit (http://silverlight.net/forums/t/28044.aspx)

    I hope someone can point out why the ListBox won't let me scroll.

    Thanks,

    Richard

    --------------------------------------
    If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.
  • sladapter

    sladapter

    All-Star

    43607 Points

    7907 Posts

    Re: Scrollbar in ListBox not working! GRRR!

    Sep 20, 2008 01:32 AM | LINK

    I tried your code, I can't make the listbox scrollbar to work either. I think the problem might be with the WrapPanel.

    I found AnimatedWrapPanel works better. You can download code from here:

    http://blogs.msdn.com/devdave/archive/2008/07/17/layout-transitions-an-animatable-wrappanel.aspx

    Try this Xaml and code:

       <Grid x:Name="LayoutRoot" Height="500" Width="400" >
            <ScrollViewer>
            <ItemsControl x:Name="myList" Background="DarkGray">
               <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>                                       
                        <controls:AnimatedWrapPanel Margin="5,5,5,5"/>                                                          
                    </ItemsPanelTemplate>               
               </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <StackPanel Orientation="Vertical" Margin="5">
                            <TextBlock Text="{Binding}"/> 
                            <Rectangle Width="100" Height="100" Fill="Red"/>                                        
                            </StackPanel>
                         </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>   
           </ScrollViewer>
        </Grid>

     public Page()
            {
                InitializeComponent();
                List<string> l = new List<string>();
                for (int i = 0; i < 50; i++)
                    l.Add(i.ToString());
                this.myList.ItemsSource = l;

           }

    But If I changed ItemsControl to ListBox, the scroll still not working correctly. Because ListBox has a built-in ScrollViewer somehow can not detect the need to scroll when using a WrapPanel.

     

     

     

     

    Sally Xu
    Software Engineer
    Aprimo, Inc

    Please remember to mark the replies as answers if they answered your question
  • rmcsharry

    rmcsharry

    Member

    380 Points

    277 Posts

    Re: Scrollbar in ListBox not working! GRRR!

    Sep 22, 2008 01:51 PM | LINK

    Hi sladapter,

    Over the weekend (before I saw your reply) I did the exact same thing. The AnimatedWrapPanel works fine, and the ScrollViewer also works fine with that.

    But if I use the ScrollViewer with the original wrap panel code, I have to give the wrap panel a height (eg. 1000) to force the scrollviewer to recognise it has something to scroll. So I have a feeling that the original wrap panel code is doing something wrong when it works out how much space it needs.

    But also it seems the ListBox scrollviewer is bugged maybe, because it refuses to scroll with either wrap panel, as you have also found.

    Never mind, at least I can get my idea to work with just the scrollviewer and the animated wrap panel.

    Thanks for confirming that I am not going nuts!

    Richard

    --------------------------------------
    If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.