【问题标题】:ListBox Child Count is 0列表框子计数为 0
【发布时间】:2012-08-10 04:18:49
【问题描述】:

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

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

然而,当我到达这条线时,我得到了一个 IndexOutOfRangeException——显然,根据 VisualTreeHelper,我的 ListBox 没有可视子项。

因为我觉得这是一个特殊情况,所以这是我的 ListBox 的 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。

【问题讨论】:

    标签: silverlight windows-phone-7


    【解决方案1】:

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

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

    至于你的其他问题:

    1. 没有应用 ControlTemplate,因为您的 ScrollViewer 没有名称。它必须命名为“ScrollViewer”。

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

    3. 要为您的内容设置 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 没有捕捉到它,所以我得到了“未指定的错误”异常令人困惑。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 2021-04-14
    相关资源
    最近更新 更多