Bodekaer
Bodekaer

Reputation: 331

WPF: ListBox click and drag selects other items

Simple question:

1) I click and hold the mouse on a ListBoxItem in a ListBox.
2) Now I drag the mouse cursor down over the next ListBoxItem in the list

It now selects this new item. I would like to disable this. So the user has to click an item to select it. Not just drag over it.

I have Single selection turned on.

Any ideas are greatly appreciated :)

Upvotes: 4

Views: 1718

Answers (2)

Matthew Walton
Matthew Walton

Reputation: 9959

This is mostly for the benefit of people like me who still need to know this.

The link majocha provided was the answer, although it's not implemented in a particularly nice way as far as I can see, because it has a hole around the MouseUp event and its boolean flag. Why do that when you can just interrogate the MouseEventArgs to find out if the button is pressed? Maybe WPF as of 2010 didn't have that capability.

So here's my code for WPF 4. You just have to handle MouseMove on the ListBox and say this:

private void ListBox_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
        (sender as ListBox).ReleaseMouseCapture();
}

Tada!

Upvotes: 3

majocha
majocha

Reputation: 1191

I guess overriding OnPreviewMouseMove will do. Read this for similar problem.

Upvotes: 0

Related Questions