使用WrapPanel和ScrollViewer在ListBox中包装图像,所选项目消失!

时间:2009-05-20 10:49:28

标签: wpf silverlight listbox scrollviewer wrappanel

我正在使用ListBox来显示800个缩略图的水平显示,但一次只能显示6个(取决于屏幕分辨率),我想使用列表两侧的按钮来浏览图像。

我目前有这个工作,但是当我将列表框的SelectedItem更改为下一个/上一个缩略图时,ScrollViewer不会自动将SelectedItem保留在视图中。 6个缩略图后的SelectedItem将会消失。

我可以移动ScrollBar来查看SelectedItem,但这还不够好,在最终版本中我甚至不想要ScrollBar,我只是希望用户能够按住左或右按钮并且它透过照片眨眼。

也只是为了告诉你为什么我想要这个,每次在ListBox上更改SelectedItem时,它都会改变上面照片的FullSize预览。

在Silverlight 2中是否存在以确保ScrollViewer中的SelectedItem(在ListBox中)保留在可视区域?

这是ListBox的当前XAML:

    <ListBox x:Name="lbPhotos" 
         ItemsSource="{Binding Photos}" 
         SelectedItem="{Binding Path=SelectedPhoto, Mode=TwoWay}" 
         ItemContainerStyle="{StaticResource ReflectionListBoxItemStyle}">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <controls:WrapPanel Margin="0" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.Template>
        <ControlTemplate>
            <Grid>
                <ScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" 
                              BorderThickness="0" MaxHeight="170">
                    <ItemsPresenter />
                </ScrollViewer>
            </Grid>
        </ControlTemplate>
    </ListBox.Template>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image MaxHeight="85" Source="{Binding ThumbnailUrl, Converter={StaticResource UrlToBitmapImageConverter}}" />
        </DataTemplate>
    </ListBox.ItemTemplate>

  </ListBox>

项目容器样式在这里:

<Style x:Key="ReflectionListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Padding" Value="3"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Background="{TemplateBinding Background}">
                        <vsm:VisualStateManager.VisualStateGroups>
                            <vsm:VisualStateGroup x:Name="CommonStates">
                                <vsm:VisualState x:Name="Normal"/>
                                <vsm:VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="MainContentBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                            <SplineColorKeyFrame KeyTime="00:00:00" Value="{StaticResource PinkColor}"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ReflectionBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                            <SplineColorKeyFrame KeyTime="00:00:00" Value="{StaticResource PinkColor}"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vsm:VisualState>
                                <vsm:VisualState x:Name="Disabled">
                                </vsm:VisualState>
                            </vsm:VisualStateGroup>
                            <vsm:VisualStateGroup x:Name="SelectionStates">
                                <vsm:VisualState x:Name="Unselected"/>
                                <vsm:VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="MainContentBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                            <SplineColorKeyFrame KeyTime="00:00:00" Value="{StaticResource PinkColor}"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ReflectionBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                            <SplineColorKeyFrame KeyTime="00:00:00" Value="{StaticResource PinkColor}"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vsm:VisualState>
                            </vsm:VisualStateGroup>
                            <vsm:VisualStateGroup x:Name="FocusStates">
                                <vsm:VisualState x:Name="Focused">
                                </vsm:VisualState>
                                <vsm:VisualState x:Name="Unfocused"/>
                            </vsm:VisualStateGroup>
                        </vsm:VisualStateManager.VisualStateGroups>
                        <StackPanel Orientation="Vertical" Margin="0,0,4,0">
                        <!-- Image -->
                                <Border HorizontalAlignment="{TemplateBinding HorizontalAlignment}" BorderThickness="3,3,3,2" CornerRadius="1" x:Name="MainContentBorder" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                                    <Border.BorderBrush>
                                        <SolidColorBrush Color="#00000000"/>
                                    </Border.BorderBrush>
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                                </Border>

                                <!-- Image reflection -->
                                <Border RenderTransformOrigin="0.5,0.5" BorderThickness="3,2,3,3" CornerRadius="1" VerticalAlignment="{TemplateBinding VerticalAlignment}" Margin="0,2,0,0" x:Name="ReflectionBorder" HorizontalAlignment="{TemplateBinding HorizontalAlignment}">
                                    <Border.BorderBrush>
                                        <SolidColorBrush Color="#00000000"/>
                                    </Border.BorderBrush>
                                    <Border.OpacityMask>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                            <GradientStop Color="#66000000" Offset="1"/>
                                            <GradientStop Color="#00000000" Offset="0.348"/>
                                        </LinearGradientBrush>
                                    </Border.OpacityMask>
                                    <Border.RenderTransform>
                                        <TransformGroup>
                                            <ScaleTransform ScaleY="-1"/>
                                        </TransformGroup>
                                    </Border.RenderTransform>
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                            </Border>
                            </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

2 个答案:

答案 0 :(得分:1)

您可以使用ScrollIntoView方法传入所选项目。

MSDN Docs

答案 1 :(得分:0)

我最终使用ScrollViewer.ScrollToHorizo​​ntalOffset,因为ScrollIntoView不起作用