繁体   English   中英

将标题添加到列表框的scrollviewer并保留virtualizingStackPanel(wp7)

[英]adding a header to a listbox's scrollviewer and keeping the virtualizingStackPanel (wp7)

我想在ListBoxes中添加一个标题,我是通过使用模板来完成的。 问题是,如果我扩展ListBox的模板,似乎列表框的virtualizingstackpanel不再按预期工作:它在我滚动它之前加载所有内容。

我在stackoverflow中发现了一些相关的问题( VirtualizingStackPanel在覆盖ScrollViewer的默认控件模板时停止工作 ),但是那里给出的解决方案无法应用于WP7:我找不到scrollviewer的名为“CanContentScroll”的属性。

我的代码

<Style x:Key="ListBoxStyle1" TargetType="ListBox">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Padding" Value="0"/>
    <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}">
                    <StackPanel>
                        <TextBlock Text="..."/>
                        <ItemsPresenter/>
                    </StackPanel>
                </ScrollViewer>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

说实话,我真的不确定这里的实际问题是什么; VirtualizingStackPanel仍然在可视化树中,但似乎没有实际添加的项目。 坏消息已经足够了,好消息是我找到了一种解决方法,通过改变ScrollViewer的默认样式来将标题放在那里,这样就产生了两种样式:

<Style x:Key="ScrollViewerStyle1" TargetType="ScrollViewer">
    <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ScrollViewer">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="ScrollStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="00:00:00.5" />
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Scrolling">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="VerticalScrollBar"
                                                        Storyboard.TargetProperty="Opacity"
                                                        To="1"
                                                        Duration="0" />
                                    <DoubleAnimation Storyboard.TargetName="HorizontalScrollBar"
                                                        Storyboard.TargetProperty="Opacity"
                                                        To="1"
                                                        Duration="0" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="NotScrolling">
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid Margin="{TemplateBinding Padding}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBlock Text="Header" Style="{StaticResource PhoneTextLargeStyle}"/>
                        <ScrollContentPresenter x:Name="ScrollContentPresenter"
                                                Grid.Row="1"
                                                Content="{TemplateBinding Content}"
                                                ContentTemplate="{TemplateBinding ContentTemplate}" />
                        <ScrollBar x:Name="VerticalScrollBar"
                                    Grid.RowSpan="2"
                                    IsHitTestVisible="False"
                                    Opacity="0"
                                    Height="Auto"
                                    Width="5"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Stretch"
                                    Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                    IsTabStop="False"
                                    Maximum="{TemplateBinding ScrollableHeight}"
                                    Minimum="0"
                                    Value="{TemplateBinding VerticalOffset}"
                                    Orientation="Vertical"
                                    ViewportSize="{TemplateBinding ViewportHeight}" />
                        <ScrollBar x:Name="HorizontalScrollBar"
                                    Grid.RowSpan="2"
                                    IsHitTestVisible="False"
                                    Opacity="0"
                                    Width="Auto"
                                    Height="5"
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Bottom"
                                    Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                    IsTabStop="False"
                                    Maximum="{TemplateBinding ScrollableWidth}"
                                    Minimum="0"
                                    Value="{TemplateBinding HorizontalOffset}"
                                    Orientation="Horizontal"
                                    ViewportSize="{TemplateBinding ViewportWidth}" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="ListBoxStyle2" TargetType="ListBox">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBox">
                <ScrollViewer x:Name="ScrollViewer"
                                Grid.Row="1"
                                Style="{StaticResource ScrollViewerStyle1}"
                                Foreground="{TemplateBinding Foreground}"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}">
                    <ItemsPresenter />
                </ScrollViewer>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM