Silverlight 列表框子计数为0

Silverlight 列表框子计数为0,silverlight,windows-phone-7,Silverlight,Windows Phone 7,我正在尝试将我在WPF中编写的控件转换为适用于Windows Phone的Silverlight。我学到了很多东西,并通过对两个版本的改进对其进行了不少调整,但在Silverlight版本中,我似乎无法将ScrollViewer从列表框中取出。从一开始就很简单: ScrollViewer s = VisualTreeHelper.GetChild(List, 0) as ScrollViewer; 然而,当我到达这一行时,我得到一个IndexOutOfRangeException——显然,根据

我正在尝试将我在WPF中编写的控件转换为适用于Windows Phone的Silverlight。我学到了很多东西,并通过对两个版本的改进对其进行了不少调整,但在Silverlight版本中,我似乎无法将ScrollViewer从列表框中取出。从一开始就很简单:

ScrollViewer s = VisualTreeHelper.GetChild(List, 0) as ScrollViewer;
然而,当我到达这一行时,我得到一个IndexOutOfRangeException——显然,根据VisualTreeHelper,我的列表框没有可视的子项

因为我觉得这是一个特例,下面是我对列表框的XAML声明:

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

我不得不注释掉ContentTemplate绑定,因为这显然是Silverlight中的只读属性?当我清理完这件事后,我还得进一步调查


我在谷歌上找不到太多的信息,大多数其他人似乎都使用了上面的方法取得了一些成功。它在WPF中当然有效。

如果您的目标只是隐藏ScrollViewer,那么您已经成功了一半。您只需在列表框上使用以下附加属性

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

...
至于你的其他问题:

  • 由于ScrollViewer没有名称,因此未应用ControlTemplate。它必须命名为“ScrollViewer”

  • 您也不能在ControlTemplate中显式设置ItemsPanel。相反,必须提供ItemsPresenter,然后设置ListBox的ItemsPanel属性

  •     <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>
    
  • 要为内容设置DataTemplate,必须在ListBox上设置ItemTemplate属性

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

  • 啊,我不认为所有这些都是我想要做的(例如,我不想隐藏任何东西,我想获得一个ScrollViewer的引用来操纵它的滚动值),但我相信你已经发现了主要问题,那就是我如何更改ListBox的ItemsPanel。我记得用过你之前展示的方法;我不知道这次为什么没做。我会在更改后报告。好吧,它似乎已经工作了——或者至少它已经部署了,尽管它不像WPF中那样工作,但这是我可以继续诊断的。你的改变很有效。我遇到的一个主要问题是,我的StackPanel上仍然有“IsItemsHost”属性,这在Silverlight中是不存在的。。。由于某种原因,Intellisense没有捕捉到它,所以我得到了“未指定的错误”异常,这让人困惑。谢谢你的帮助。