简体   繁体   中英

Scrolling with mouse wheel in ListBox with modified ItemsPanel

I have the following ListBox defined in my WPF app:

<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. However, this causes me to loose ability to scroll through the ListBox items with the mouse wheel.

Is there any solution to keep this customization yet have the mouse scrolling functionality?

As asked by eran, the code to add items follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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