9

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

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

第一张截图: 查看 1

第二张截图: 查看 2

我的列表视图样式:

 <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  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>

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

谢谢大家的努力。

4

1 回答 1

15

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

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

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

有关更多信息,请访问此处:http: //msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.cancontentscroll.aspx

于 2012-07-17T18:16:43.277 回答