A-Type
A-Type

Reputation: 1148

ListBox Child Count is 0

I'm trying to translate a control I wrote in WPF into Silverlight for Windows Phone. I've learned a lot and tweaked it quite a bit with improvements made to both versions, but I just can't seem to get the ScrollViewer out of a ListBox in the Silverlight version. It seemed pretty simple from the outset:

ScrollViewer s = VisualTreeHelper.GetChild(List, 0) as ScrollViewer;

However, when I reach this line, I get an IndexOutOfRangeException-- apparently, according to VisualTreeHelper, my ListBox has no visual children.

Since I get the feeling this is a special case, here's my XAML declaration of the ListBox:

<ListBox x:Name="List" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
             ItemsSource="{Binding ItemsSource, ElementName=SnapListControl}"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.Style>
            <Style TargetType="ListBox">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBox">
                            <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
                                <VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True"
                                        Margin="{Binding ActualWidth, ElementName=LayoutRoot, Converter={StaticResource Hc}}">                                                                                
                                </VirtualizingStackPanel>
                            </ScrollViewer>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.Style>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="RenderTransformOrigin">
                    <Setter.Value>
                        <Point X="0.5" Y="0.5"/>
                    </Setter.Value>
                </Setter>
                <Setter Property="Padding" Value="0"/>
                <!--<Setter Property="ContentTemplate" Value="{Binding ItemContentTemplate, ElementName=SnapListControl}"/>-->
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

I had to comment out the ContentTemplate binding because apparently that's a readonly property in Silverlight? I'll have to investigate that more when I finish clearing this up.

I can't find much from googling this, most other people seem to use the method above to some success. It certainly works in WPF.

Upvotes: 0

Views: 242

Answers (1)

Silver Solver
Silver Solver

Reputation: 2320

If your objective is simply to hide the ScrollViewer, you are already half way there. You simply need to use the following attached properties on your ListBox

<ListBox ScrollViewer.VerticalScrollBarVisibility="Hidden"
        ScrollViewer.HorizontalScrollBarVisibility="Hidden" >
...

As for your other problems:

  1. The ControlTemplate is not being applied because your ScrollViewer does not have a name. It must be named "ScrollViewer".

  2. You cannot explicitly set your ItemsPanel in the ControlTemplate either. Instead, you must provide an ItemsPresenter, and then set the ItemsPanel property of the ListBox.

  3. To set the DataTemplate for your content, you must set the ItemTemplate property on the ListBox.

        <ListBox Height="100" Margin="200,195,156,0" 
                VerticalAlignment="Top" 
                ScrollViewer.VerticalScrollBarVisibility="Hidden"
                ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
            <ListBox.Style>
                <Style TargetType="ListBox">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBox">
                                <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
                                    <ItemsPresenter/>
                                </ScrollViewer>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.Style>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal" 
                                    Margin="{Binding ActualWidth, ElementName=LayoutRoot, Converter={StaticResource Hc}}">                                                                                
                    </VirtualizingStackPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border Width="100" Height="100" Background="White">
                        ...
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    

Upvotes: 1

Related Questions