【问题标题】:WPF scroll do not working on ListBox inside ItemsControlWPF滚动不适用于ItemsControl内的ListBox
【发布时间】:2017-03-30 06:12:10
【问题描述】:

需要使用扩展器构建图像浏览器,但滚动有一些问题。我有里面有 ListBox 的 ItemsControl,当鼠标在 ListBox 上时滚动不起作用。这是 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>

【问题讨论】:

标签: wpf scroll listbox


【解决方案1】:

用这个改变 xaml:

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

【讨论】:

    【解决方案2】:

    当您选择列表框图像时,滚动查看器会失去焦点。 因此,您可以在 MouseWheel/PreviewMouseWheel 事件上将焦点设置为滚动查看器,也可以像下面这样手动滚动。

    myScroll.ScrollToVerticalOffset(myScroll.VerticalOffset + 10);
    

    【讨论】:

    • 我将 PreviewMouseWheel 添加到 ListBox: private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { mainScroll.ScrollToVerticalOffset(mainScroll.VerticalOffset - e.Delta);这是可行的,谢谢 Maulik :)
    【解决方案3】:

    您只想滚动 ScrollViewer 吗?那你为什么在 ItemsControl 中使用 ListBox 呢?我看到你阻止了 ListBox 的滚动

    ScrollViewer.CanContentScroll="False"
    

    但是 ListBox 在他的模板中有 ScrollViewer。而且我认为这个 ScrollViewer 处理“鼠标滚轮”事件并且它没有到达根 ScrollViewer。 我认为只要将 ListBox 替换为 ItemsControl,就可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2013-02-24
      • 2021-05-12
      • 1970-01-01
      相关资源
      最近更新 更多