1

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

user2452157
  • 313
  • 2
  • 13
  • I tried it on my machine and it works fine. Interesting. – Lukas Aug 15 '19 at 11:14
  • @Lukas: Did you click into the list and hold the button while moving it below the control? What happend then? did it scroll to the end? I'll try on other machines, too. – user2452157 Aug 15 '19 at 11:29
  • Yep, I can reproduce it now. How bizarre; seems like the code is getting stuck in System.Threading.WaitHandle.InternalWaitOne. Some kind of deadlocking issue. – Lukas Aug 15 '19 at 12:16
  • There is also a steady cpu usage, and an increase in memory to some extend. With 1000 items instead of 10000 it takes some (>10s) time but finally it finishes. Seems to be some performance problem. – user2452157 Aug 16 '19 at 08:57

0 Answers0