滚动分组列表视图

时间:2012-07-17 18:14:03

标签: wpf listview scrollbar grouping

我非常绝望......让我的WPF分组列表视图工作...... 我无法在列表视图中滚动...

在我的示例应用程序中,我有一个带有两个扩展器的Listview。 第一个包含很多listviewitems ...所以滚动是必要的... 但是,如果我使用“该死的滚动条”,它会直接跳转到下一个扩展器(在我的情况下是扩展器2of2)...所以无法正确滚动...

第一个截图: View 1

第二个截图: View 2

我的ListView样式:

 <Style x:Key="list12" TargetType="ListView">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListView">
                <Border Name="Border" BorderThickness="1" BorderBrush="#999999" Background="#DFDFDF">
                    <ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
                        <ItemsPresenter />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的GridView.GridViewScrollViewerStyle:

<Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="ScrollViewer">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ScrollViewer">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <DockPanel Margin="{TemplateBinding Padding}">
                        <ScrollViewer DockPanel.Dock="Top" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Focusable="false">
                            <GridViewHeaderRowPresenter Margin="0,0,0,0" Columns="{Binding Path=TemplatedParent.View.Columns,
                                RelativeSource={RelativeSource TemplatedParent}}" ColumnHeaderContainerStyle="{Binding Path=TemplatedParent.View.ColumnHeaderContainerStyle, RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderTemplate="{Binding Path=TemplatedParent.View.ColumnHeaderTemplate,RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderTemplateSelector="{Binding Path=TemplatedParent.View.ColumnHeaderTemplateSelector, RelativeSource={RelativeSource TemplatedParent}}"
                                AllowsColumnReorder="{Binding Path=TemplatedParent.View.AllowsColumnReorder, RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderContextMenu="{Binding Path=TemplatedParent.View.ColumnHeaderContextMenu, RelativeSource={RelativeSource TemplatedParent}}"
                                ColumnHeaderToolTip="{Binding Path=TemplatedParent.View.ColumnHeaderToolTip, RelativeSource={RelativeSource TemplatedParent}}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                        <ScrollContentPresenter Name="PART_ScrollContentPresenter"
                                KeyboardNavigation.DirectionalNavigation="Local"
                                CanContentScroll="True" CanHorizontallyScroll="False"
                                CanVerticallyScroll="False"/>
                    </DockPanel>
                    <ScrollBar Name="PART_HorizontalScrollBar" Orientation="Horizontal" Grid.Row="1" Maximum="{TemplateBinding ScrollableWidth}" ViewportSize="{TemplateBinding ViewportWidth}" Value="{TemplateBinding HorizontalOffset}" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
                    <ScrollBar Name="PART_VerticalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" ViewportSize="{TemplateBinding ViewportHeight}" Value="{TemplateBinding VerticalOffset}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的ListViewItemContainerStyle:

<Style x:Key="ListViewItemContainerStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="#ffffff" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="Margin" Value="0,0,0,0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border x:Name="Bd" Background="{TemplateBinding Background}" SnapsToDevicePixels="true" BorderThickness="0,0,0,1" BorderBrush="#6FBDE8">
                        <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected"  Value="true">
                            <Setter TargetName="Bd" Property="BorderBrush" Value="#FF143c65" />
                            <Setter Property="Background" TargetName="Bd">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <LinearGradientBrush.GradientStops>
                                            <GradientStopCollection>
                                                <GradientStop Color="#FF75aac7" Offset="0"/>
                                                <GradientStop Color="#FF143c65" Offset="1"/>
                                            </GradientStopCollection>
                                        </LinearGradientBrush.GradientStops>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="Bd" Property="Background" Value="#e0eff8" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true" />
                                <Condition Property="Selector.IsSelectionActive" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <LinearGradientBrush.GradientStops>
                                            <GradientStopCollection>
                                                <GradientStop Color="#FF75aac7" Offset="0"/>
                                                <GradientStop Color="#FF143c65" Offset="1"/>
                                            </GradientStopCollection>
                                        </LinearGradientBrush.GradientStops>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" TargetName="Bd" Value="#FF143c65"/>
                            <Setter Property="Foreground" Value="White"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

只是我的Listview:

<ListView  Style="{StaticResource list12}" Name="ListView1" ItemsSource="{Binding}" ItemContainerStyle="{DynamicResource ListViewItemContainerStyle}" Margin="0,44,0,0">
                        <ListView.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.ContainerStyle>
                                    <Style TargetType="{x:Type GroupItem}">
                                        <Setter Property="Template" >
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                                    <Expander IsExpanded="true" BorderThickness="0,0,0,1" Margin="10">
                                                            <Expander.Header>
                                                            <Grid>
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition/>
                                                                    <ColumnDefinition Width="10"/>
                                                                    <ColumnDefinition/>
                                                                </Grid.ColumnDefinitions>
                                                                <Label Content="{Binding Path=Name}" Grid.Column="0"/>
                                                                <Label Content="{Binding Path=ItemCount}" FontWeight="Bold" Grid.Column="2"/>
                                                            </Grid>
                                                        </Expander.Header>
                                                            <Expander.Content>
                                                                <ItemsPresenter />                                              
                                                            </Expander.Content>
                                                        </Expander>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </GroupStyle.ContainerStyle>
                                </GroupStyle>
                            </ListView.GroupStyle>
                        <ListView.View>
                        <GridView>
                            <GridViewColumn CellTemplate="{StaticResource IDItemTemplate}" Header="ID"/>
                            <GridViewColumn CellTemplate="{StaticResource DateItemTemplate}" Header="Datum" />
                            <GridViewColumn CellTemplate="{StaticResource TypeItemTemplate}" Header="Typ" />
                            <GridViewColumn CellTemplate="{StaticResource StatusItemTemplate}" Header="Status" />
                            <GridViewColumn CellTemplate="{StaticResource AuthorItemTemplate}" Header="Autor" />
                            <GridViewColumn CellTemplate="{StaticResource BenennungItemTemplate}" Header="Benennung" />
                        </GridView>
                    </ListView.View>
                </ListView>

我不知道如何解决我的问题...... 希望你们中的一些人可以帮助我让它发挥作用......

谢谢大家的努力。

1 个答案:

答案 0 :(得分:11)

您的问题是ListView样式的<Setter Property="ScrollViewer.CanContentScroll" Value="true"/> setter属性。

ScrollViewer中的内容可以按物理单位或逻辑单位滚动。物理单位是与设备无关的像素。逻辑单元用于滚动ItemsControl中的项目。 (这就是你现在正在做的事)。 ScrollViewer的默认行为是使用物理单位滚动其内容。但是,如果 CanContentScroll设置为true,则内容可以使用逻辑单元进行滚动。例如,ListBox,ListView和从ItemsControl继承的其他控件使用逻辑单元进行滚动。如果CanContentScroll为true,则ExtentHeight,ScrollableHeight,ViewportHeight和VerticalOffset属性的值是项目数,而不是物理单位。

如果需要物理滚动而不是逻辑滚动,请将主机Panel元素包装在ScrollViewer中,并将其CanContentScroll属性设置为false。物理滚动是大多数Panel元素的默认滚动行为。

有关详情,请访问:http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.cancontentscroll.aspx