为什么 WPF 列表框在鼠标按下而不是按下按钮时更改选择? [英] Why does the WPF listbox change selection on mouse button down rather than button up?

查看:29
本文介绍了为什么 WPF 列表框在鼠标按下而不是按下按钮时更改选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未注意到这一点,但是当鼠标按下时,WPF ListBox 似乎会更改其 SelectedItem,但尚未释放.作为一个简单的示例,只需创建一个包含多个 ListBoxItem 的简单 ListBox,如下所示:

I had never noticed this before, but the WPF ListBox seems to change its SelectedItem when the Mouse is down, but has not yet been released. As a quick example, just create a simple ListBox with several ListBoxItems, like so:

<ListBox>
  <ListBoxItem>Hello</ListBoxItem>
  <ListBoxItem>World</ListBoxItem>
  <ListBoxItem>ListBox</ListBoxItem>
  <ListBoxItem>Test</ListBoxItem>
</ListBox>

启动您的应用程序,按下鼠标按钮(不要松开它!)然后移动鼠标.SelectedItem 将随着鼠标的移动而改变.这说明了更大的问题(至少对我而言),ListBox 的 SelectedItem 将在您按下鼠标时设置,而不是在鼠标按下时设置.通常这不是问题,但在我的情况下,我想启用拖动 &拖放到我的 ListBox 中的项目上,而不显式选择这些项目.

fire up your application, press the mouse button (don't release it!) and move the mouse around. The SelectedItem will change as the mouse moves. This illustrates the larger problem (for me, at least), that a ListBox's SelectedItem will be set as soon as you mouse down, not when mouse up occurs. Usually that isn't a problem, but in my case I'd like to enable drag & drop on the items in my ListBox, without the items explicitly becoming selected.

我想我唯一的办法是构建一个自定义 ItemsControl 或 Selector,其选择样式语义类似于 ListBox,所以我的问题实际上更多,为什么 ListBox 会以这种方式工作?有人对此有任何见解吗?

I imagine my only recourse is to build a custom ItemsControl or Selector with selection-style semantics similar to ListBox, so really my question is more, why does ListBox work this way? Does anyone have any insight into this?

推荐答案

这可能有点跑题了,但我刚刚遇到了类似的问题.我不想进行拖放,但我想在 MouseUp 而不是 MouseDown 上选择 ListBox 上的项目.虽然 Sheena 伪代码可能会给出一些提示,但我还是花了一段时间才找到正确的解决方案.所以这是我的问题的解决方案.

It might be a bit off topic but i just came up to similar problem. I do not want to do drag and drop but i want to select items on ListBox on MouseUp and not MouseDown. Although Sheena pseudo code might give some hint it still took me a while before i found out right solution. So this is my solution for my problem.

public class ListBoxSelectionItemChangedOnMouseUp : ListBox
{
    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
        {
            DependencyObject obj = this.ContainerFromElement((Visual)e.OriginalSource);
            if (obj != null)
            {
                FrameworkElement element = obj as FrameworkElement;
                if (element != null)
                {
                    ListBoxItem item = element as ListBoxItem;
                    if (item != null && this.Items.Contains(item))
                    {
                        this.SelectedItem = item;
                    }
                }
            }
        }
    }

    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        e.Handled = true;
    }
}

我还想只在鼠标左键上进行选择.在拖放的情况下,有必要在鼠标按下事件中保存所选项目,然后在鼠标按下事件中使用它.我希望这会对某人有所帮助.

I also wanted to select only on left mouse button. In case of drag and drop its necessary to save selected item in mouse down event and then use it in mouse up event. I hope this will help someone.

这篇关于为什么 WPF 列表框在鼠标按下而不是按下按钮时更改选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆