开发者

WPF: ListBox click and drag selects other items

开发者 https://www.devze.com 2022-12-27 13:19 出处:网络
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

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 :)


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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消