WPF scroll do not working on ListBox inside ItemsC

2019-09-07 22:14发布

问题:

Need to build image explorer with expanders, but I have some problem with scrolling. I have ItemsControl with ListBox inside, scroll do not work when mouse on ListBox. Here is xaml:

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Grid.Row="1" Background="{DynamicResource LightGrayBackgroundBrush}" >
    <ItemsControl x:Name="itmsControl"  DataContext="{Binding ElementName=_self}" ItemsSource="{Binding ImagesSource}"  Margin="15" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="grdIn">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" MinHeight="25"/>
                        <RowDefinition x:Name="grd1"/>
                    </Grid.RowDefinitions>

                    <Expander Grid.Row="1" IsExpanded="True" BorderThickness="0" Background="White">
                        <Expander.Header>
                            <Border Background="White" BorderBrush="White" Height="40">
                                <TextBlock Text="{Binding Date}" Background="White" FontSize="14" Foreground="Gray" FontWeight="Bold" VerticalAlignment="Center" Margin="10,0,0,0"/>
                            </Border>
                        </Expander.Header>

                        <ListBox ItemsSource="{Binding ImageList}" ItemContainerStyle="{DynamicResource ImageListBoxItemStyle}"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionMode="Extended" Background="Transparent" SelectionChanged="ListBox_SelectionChanged" PreviewKeyDown="OnKeyDownHandler" MouseDown="ListBox_MouseDown" ScrollViewer.CanContentScroll="False">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Image Stretch="UniformToFill" Width="{Binding Width}" Height="{Binding Height}" Source="{Binding Source}" Margin="3" MouseDown="Image_MouseDown"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel IsItemsHost="True" Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                        </ListBox>
                    </Expander>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

回答1:

When you select Listbox image, focus is lost for scroll viewer. So, you can set focus to scroll viewer on MouseWheel/PreviewMouseWheel event or you can scroll manually like below.

myScroll.ScrollToVerticalOffset(myScroll.VerticalOffset + 10);


回答2:

Do you want to scroll ScrollViewer only? Then why do you use ListBox inside ItemsControl? I watch that you block scrolling of ListBox

ScrollViewer.CanContentScroll="False"

But ListBox has ScrollViewer inside his template. And I think that this ScrollViewer handle "mouse whell" event and it does not reach the root ScrollViewer. I think that you can fix your problem if you just replace ListBox to ItemsControl.



回答3:

change xaml with this:

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <ScrollViewer>
            <WrapPanel IsItemsHost="True" Orientation="Horizontal" />
        </ScrollViewer>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>