Horizontal Autoscroll on ListBox is blocking

67 views Asked by At

I want to implement a rubber-band selection of items in a wpf horizontal ListBox. In principle it works, except that with many items in the list, the program freezes for a long time or even forever when the mouse is moved also below the control. Here is the simplest code to reproduce:

<Window x:Class="DemonstrateWpfDeadlock.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <ListBox SelectionMode="Multiple" ItemsSource="{Binding .}" VerticalAlignment="Top" 
             MouseDown="List_OnMouseDown"
             MouseUp="List_OnMouseUp">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Grid>

and code behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var objs = new List<string>();
        for (int i =0; i < 10000; i++)
            objs.Add(i.ToString());
        DataContext = objs;
    }

    private void List_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        Mouse.Capture((IInputElement) sender);
    }

    private void List_OnMouseUp(object sender, MouseButtonEventArgs e)
    {
        ((IInputElement)sender).ReleaseMouseCapture();
    }
}

When dragging the mouse below the control it freezes. With the StackPanel set to vertical everything is smooth! But unfortunately it has to be horizontal. Is this a quirk in Wpf? Is there a workaround? I tried to use .NET 4.8 instead of 4.6.1 but no change. Windows 10 1903 x64

0

There are 0 answers