Xaml child listview autoscroll

时间:2018-01-12 14:43:32

标签: wpf xaml listview scroll

这是.XAML:

<Window x:Class="IMManager.ImManagerWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="IMM Media Manager" 
        SnapsToDevicePixels="True" 
        WindowState="Normal" 
        MaxWidth="720" 
        ResizeMode="NoResize" 
        WindowStartupLocation="CenterScreen">

    <Window.Resources>
        <Style x:Key="AlternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <!-- setting up triggers for alternate background colors -->
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="LightGray"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="2">
                    <Setter Property="Background" Value="White"></Setter>
                </Trigger>
            </Style.Triggers>
            <!-- setting row height here -->
            <Setter Property="Height" Value="25" />
        </Style>

    </Window.Resources>
  <Grid>
      <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
        <ListView x:Name="LvAlbums" ItemsSource="{Binding XPath=/downloads/Album}" Width="680" ScrollViewer.CanContentScroll="False" >
        <ListView.ItemTemplate>
            <DataTemplate x:Name="dt1">
                <StackPanel x:Name="spAlbum" HorizontalAlignment="Left">
                    <TextBlock 
                        Text="{Binding XPath=@Artist,StringFormat='Artist: {0}'}"
                        FontSize="16px"
                        />
                    <TextBlock 
                        Text="{Binding XPath=@Name,StringFormat='Album: {0}'}"
                        FontSize="16px"
                        />
                    <ListView x:Name="lvTracks" ItemsSource="{Binding XPath=Item}" ItemContainerStyle="{StaticResource AlternatingListViewItemStyle}"   AlternationCount="2">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Track" Width="460">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock x:Name="trackName" Text="{Binding XPath=@Name}" Width="450" LineHeight="25" Height="25" VerticalAlignment="Center" Margin="0,3,0,0">
                                            </TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Progress" Width="175">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Canvas VerticalAlignment="Center" Height="25">
                                                <ProgressBar Foreground="GreenYellow" Name="PbStatus" Value="{Binding XPath=@Progress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Minimum="0" Maximum="100" Height="20" VerticalAlignment="Center"  Margin="0,3,0,0" Width="165"/>
                                                <TextBlock FontFamily="Segoe UI" LineHeight="25" FontWeight="Bold"  Text="{Binding XPath=@Info, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="20" Width="165" VerticalAlignment="Center" Margin="0,4,0,0"/>
                                            </Canvas>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
      <Label Name="lblStatus" Grid.Row="1" FontSize="16"></Label>
  </Grid>
</Window>

这是它的样子:

Album Download Screenshot

问题是当前下载曲目超出屏幕底部时,它不会滚动到视图中。

我试图以某种方式使用lvTracks.ScrollIntoView,但我在解决lvTracks方面遇到了麻烦。我可以解决LvAlbums,但这对我没有好处。

可以有多个相册,每个相册都有自己的lvTracks列表视图。

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用ListViewItemContainerGenerator类获取对外部可见行的内部VisualTreeHelper的引用,例如:

private void FindInnerListView(object sender, RoutedEventArgs e)
{
    int rowIndex = 0; //find the lvTracks at the first row (index = 0) of LvAlbums
    ListBoxItem lbi = LvAlbums.ItemContainerGenerator.ContainerFromIndex(rowIndex) as ListBoxItem;
    if (lbi != null)
    {
        ListView lvTracks = GetChildOfType<ListView>(lbi);
        if (lvTracks != null)
        {
            //lvTracks.ScrollIntoView();
        }
    }
}

private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj == null) return null;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        var child = VisualTreeHelper.GetChild(depObj, i);

        var result = (child as T) ?? GetChildOfType<T>(child);
        if (result != null) return result;
    }
    return null;
}

在调用ContainerFromIndexContainerFromItem方法之前,请确保已实际创建了容器。

答案 1 :(得分:0)

毕竟我有一个“哦,呃”的时刻。我只是将所有内容都放在ScrollView(SvMain)中并使用了SvMain.ScrollToVerticalOffset。

仍然,mm8回答了实际问题,并得到了赞誉。