【问题标题】:Automatic Scrolling in a Silverlight List Box with Custom Template使用自定义模板在 Silverlight 列表框中自动滚动
【发布时间】:2011-11-21 08:35:50
【问题描述】:

我有带有自定义模板的 ListBox,如何以编程方式将其向下滚动到底部?

Automatic Scrolling in a Silverlight List Box 描述了滚动到 ListBox 底部的方法。不幸的是,此方法不适用于带有自定义样式模板的 ListBox。

有没有人成功用自定义样式滚动列表框?

问题代码:

<Grid.Resources>
    <Style x:Key="HorizontalWrapListBox" TargetType="ListBox">
        <Style.Setters>
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <ScrollViewer>
                            <ItemsPresenter />
                        </ScrollViewer>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</Grid.Resources>

<ListBox x:Name="MyListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}"
         Style="{StaticResource HorizontalWrapListBox}">
    <ListBox.ItemTemplate>
        <DataTemplate>
             <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                 <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" 
                         Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                 <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" 
                         Margin="12,-6,12,0" 
                         Style="{StaticResource PhoneTextSubtleStyle}"/>
             </StackPanel>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

【问题讨论】:

    标签: silverlight listbox controltemplate


    【解决方案1】:

    您需要正确命名您的模板部分,这才可以开始工作。 ScrollViewer 应命名为 x:Name="ScrollViewer"。检查

    1. ListBox Styles and Templates,
    2. Customizing the Appearance of an Existing Control by Using a ControlTemplate,
    3. TemplatePartAttribute

    【讨论】:

    • 谢谢,伙计!它完美地工作!可悲的是,由于我的声誉,我无法投票给答案:-[
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    相关资源
    最近更新 更多