【问题标题】:ListBox with GroupStyle scrolls by group, not by item具有 GroupStyle 的 ListBox 按组滚动,而不是按项目滚动
【发布时间】:2021-12-22 05:05:22
【问题描述】:

我有一个ListBox 和一个GroupStyle。如果我使用GroupStyleScrollViewerListBox 按组滚动而不是那个项目。因此,如果该组的项目数量超过屏幕上的容量,则用户永远不会看到屏幕外的项目。如果我将GroupStyle 注释掉,ScrollViewer 的工作方式就像是逐项滚动。 (我添加了ControlTemplate 来尝试不同的东西)。

<UserControl.Resources>
    <CollectionViewSource x:Key="BusStopsFlattend" Source="{Binding BusStopsFlat}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="CountryCode" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</UserControl.Resources>

<ListBox Grid.Row="1" ItemsSource="{Binding Source={StaticResource BusStopsFlattend}}" Margin="48,0,48,60">
<ListBox.Template>
    <ControlTemplate TargetType="{x:Type ListBox}">
        <ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Disabled">
            <ItemsPresenter />
        </ScrollViewer>
    </ControlTemplate>
</ListBox.Template>

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid Margin="0,24,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <controls:IconViewbox Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" IconData="{StaticResource IconPathPencilOutline}" IconSize="72" Margin="0,0,24,0" />

            <TextBlock Grid.Column="1" Grid.Row="0" FontSize="32" TextWrapping="Wrap" Text="Name" /> 
            <TextBlock Grid.Column="1" Grid.Row="1" FontSize="22" TextWrapping="Wrap" Text="Address" />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel />
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

<ListBox.GroupStyle>
    <GroupStyle>
        <GroupStyle.Panel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </GroupStyle.Panel>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <TextBlock Grid.Row="0" FontSize="36" FontWeight="Bold" Margin="0,48,0,24" Text="{Binding Name}" />
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListBox.GroupStyle>
</ListBox>

【问题讨论】:

    标签: c# wpf listbox scrollviewer


    【解决方案1】:

    只需将ScrollViewer.CanContentScroll 属性设置为False 即可使用物理滚动而不是逻辑滚动。请记住,如果您决定使用虚拟化,这将禁用它。

    【讨论】:

    • 但我想使用逻辑滚动。不是按组,而是按组中的每个项目。
    • 最后我没有找到让它工作的方法。所以我设置了ScrollViewer.CanContentScroll = false 并手动计算了ListBoxItem 的平均高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多