使用带有修改过的ItemsPanel的ListBox中的鼠标滚轮滚动

时间:2014-09-02 18:20:29

标签: c# wpf xaml

我的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>

我想更改ItemsPanel以使用VirtualizingStackPanel获得性能并更改ListBox方向(注释掉的部分) - ListBoxItems是缩略图。但是,这会导致我无法使用鼠标滚轮滚动ListBox项目。

是否有任何解决方案可以保留此自定义但还具有鼠标滚动功能?

正如伊兰所说,添加项目的代码如下:

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);
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过将HorizontalScrollBarVisibility设为Visible

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Visible">