简体   繁体   English

在具有修改后的ItemsPanel的ListBox中使用鼠标滚轮滚动

[英]Scrolling with mouse wheel in ListBox with modified ItemsPanel

I have the following ListBox defined in my WPF app: 我在WPF应用程序中定义了以下ListBox:

<ListBox DockPanel.Dock="Bottom" Height="105" Name="ThumbnailsList" Background="#80FFFFF0"
         SelectionChanged="ThumbnailsList_SelectionChanged">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

I wanted to change ItemsPanel to use VirtualizingStackPanel for performance and to change ListBox orientation (the part commented out) - the ListBoxItems are thumbnail images. 我想更改ItemsPanel以使用VirtualizingStackPanel来提高性能,并更改ListBox的方向(注释掉的部分)-ListBoxItems是缩略图。 However, this causes me to loose ability to scroll through the ListBox items with the mouse wheel. 但是,这使我无法使用鼠标滚轮滚动浏览ListBox项。

Is there any solution to keep this customization yet have the mouse scrolling functionality? 有什么解决方案可以保留此自定义功能,但具有鼠标滚动功能?

As asked by eran, the code to add items follows: 按照eran的要求,添加项目的代码如下:

foreach (string filename in Images)
{
    Image img = new Image();
    BitmapImage bmp = new BitmapImage();
    try
    {
        using (FileStream stream = File.OpenRead(filename))
        {
            bmp.BeginInit();
            bmp.CacheOption = BitmapCacheOption.OnLoad;
            bmp.DecodePixelHeight = 75;
            bmp.StreamSource = stream;
            bmp.EndInit();
        }
    }
    catch (Exception ex)
    {
        //commented out
    }

    Border b = new Border();
    b.BorderBrush = Brushes.AntiqueWhite;
    b.BorderThickness = new Thickness(1);
    b.Child = img;
    b.Effect = ef; //this is System.Windows.Media.Effects.DropShadowEffect
    img.SnapsToDevicePixels = true;
    img.Source = bmp;
    img.Height = 75;
    img.ToolTip = filename.Substring(lastSlash + 1);
    ThumbnailsList.Items.Add(b);
}

您是否尝试过将HorizontalScrollBarVisibility设置为Visible

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Visible">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM