簡體   English   中英

在 WPF 中滾動時的動畫列表框項

[英]Animated ListBox Item when scrolling in WPF

我需要建議。 如何通過滾動在 ListBox 中創建項目的 animation,請參見圖片。 使用 WrapPanel 時,我需要實現相同的 animation。 我會很高興得到任何建議。

滾動時動畫淡入淡出animated fade when scrolling

這是代碼

 <!--VirtualizingStackPanel.IsVirtualizing="True"
    VirtualizingStackPanel.CacheLengthUnit="Pixel"
    VirtualizingStackPanel.CacheLength="100,100"
    VirtualizingStackPanel.ScrollUnit="Pixel"
    VirtualizingStackPanel.VirtualizationMode="Recycling"-->

    <ListBox x:Name="ListBox1" ItemsSource="{Binding MoviesCvs.View,IsAsync=True}" 
             Style="{StaticResource CommonListBoxStyle}"
             HorizontalContentAlignment="Stretch"
             IsSynchronizedWithCurrentItem="False"
             ScrollViewer.CanContentScroll="True"
             ScrollViewer.VerticalScrollBarVisibility="Auto"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <!--<VirtualizingStackPanel IsItemsHost="True"/>-->
                <WrapPanel Orientation="Vertical" IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0"  Background="Transparent"  Height="460" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}">
                    <Grid Background="Transparent" HorizontalAlignment="Left" Width="250" Height="460" Margin="0">
                        <StackPanel VerticalAlignment="Top">
                            <cachedImage:Image Stretch="Uniform" ImageUrl="{Binding PosterPath}" >
                                <cachedImage:Image.Triggers>
                                    <!-- Opacity animation -->
                                    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.8" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </cachedImage:Image.Triggers>
                                  <!-- ********************** -->
                            </cachedImage:Image>
                            <TextBlock Margin="5,10,5,0" Text="{Binding MovieTitle}" TextTrimming="CharacterEllipsis" Foreground="Black" TextWrapping="Wrap" MaxHeight="50" FontSize="17"/>
                        </StackPanel>
                    </Grid>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

下載源代碼

我以某種方式解決了它。 我發現了這個: http://munnaondotnet.blogspot.com/2011/09/is-item-is-visible-in-scroll-viewer.html

然后我只是編輯了一點 xaml

 <ListBox.Template>
            <ControlTemplate>
                <ScrollViewer x:Name="ScrollViewer1" ScrollChanged="HandleScrollChanged"
                              CanContentScroll="True" 
                              ScrollViewer.VerticalScrollBarVisibility="Auto"
                              ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                    <ItemsPresenter />
                </ScrollViewer>
            </ControlTemplate>
        </ListBox.Template>

<Grid x:Name="GridRoot" Width="250" Height="460" Background="Red" >
                        <Grid.Style>
                            <Style TargetType="{x:Type Grid}">
                                <Setter Property="Visibility" Value="Collapsed"/>
                                <Setter Property="Opacity" Value="0"/>
                                <Style.Triggers>
                                    <Trigger Property="Visibility" Value="Visible">
                                        <Trigger.EnterActions>
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.8" />
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </Trigger.EnterActions>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Style>
                    </Grid>

和后面的代碼

  private void HandleScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        ShowVisibleItems(sender);

    }

    private void ShowVisibleItems(object sender)
    {
        var scrollViewer = (FrameworkElement)sender;
        foreach (var item in this.ListBox1.Items)
        {
            var listBoxItem = (FrameworkElement)this.ListBox1.ItemContainerGenerator.ContainerFromItem(item);
            ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
            DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;

            Grid target = (Grid)myDataTemplate.FindName("GridRoot", myContentPresenter);
            if (IsFullyOrPartiallyVisible(listBoxItem, scrollViewer))
            {
                target.Visibility = Visibility.Visible;
            }
            else
            {
                target.Visibility = Visibility.Collapsed;
            }

        }
    }
    private bool IsFullyOrPartiallyVisible(FrameworkElement element, FrameworkElement container)
    {
        if (element == null || container == null)
            return false;
        if (element.Visibility != Visibility.Visible)
            return false;
        Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
        Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
        return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM