<i id='gmhrQ'><tr id='gmhrQ'><dt id='gmhrQ'><q id='gmhrQ'><span id='gmhrQ'><b id='gmhrQ'><form id='gmhrQ'><ins id='gmhrQ'></ins><ul id='gmhrQ'></ul><sub id='gmhrQ'></sub></form><legend id='gmhrQ'></legend><bdo id='gmhrQ'><pre id='gmhrQ'><center id='gmhrQ'></center></pre></bdo></b><th id='gmhrQ'></th></span></q></dt></tr></i><div id='gmhrQ'><tfoot id='gmhrQ'></tfoot><dl id='gmhrQ'><fieldset id='gmhrQ'></fieldset></dl></div>
  • <small id='gmhrQ'></small><noframes id='gmhrQ'>

      <tfoot id='gmhrQ'></tfoot>

      <legend id='gmhrQ'><style id='gmhrQ'><dir id='gmhrQ'><q id='gmhrQ'></q></dir></style></legend>

        <bdo id='gmhrQ'></bdo><ul id='gmhrQ'></ul>
      1. 为什么 WPF 列表框在鼠标按下而不是按下按钮时更改选择?

        时间:2023-10-08

          <small id='5EaQO'></small><noframes id='5EaQO'>

            <i id='5EaQO'><tr id='5EaQO'><dt id='5EaQO'><q id='5EaQO'><span id='5EaQO'><b id='5EaQO'><form id='5EaQO'><ins id='5EaQO'></ins><ul id='5EaQO'></ul><sub id='5EaQO'></sub></form><legend id='5EaQO'></legend><bdo id='5EaQO'><pre id='5EaQO'><center id='5EaQO'></center></pre></bdo></b><th id='5EaQO'></th></span></q></dt></tr></i><div id='5EaQO'><tfoot id='5EaQO'></tfoot><dl id='5EaQO'><fieldset id='5EaQO'></fieldset></dl></div>

                <tbody id='5EaQO'></tbody>
              <legend id='5EaQO'><style id='5EaQO'><dir id='5EaQO'><q id='5EaQO'></q></dir></style></legend>

              • <bdo id='5EaQO'></bdo><ul id='5EaQO'></ul>
                1. <tfoot id='5EaQO'></tfoot>
                  本文介绍了为什么 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 列表框在鼠标按下而不是按下按钮时更改选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ASP:ListBox 获取选定的项目 - 一个衬里? 下一篇:更改选定颜色列表框

                  相关文章

                    • <bdo id='NeT45'></bdo><ul id='NeT45'></ul>
                    <i id='NeT45'><tr id='NeT45'><dt id='NeT45'><q id='NeT45'><span id='NeT45'><b id='NeT45'><form id='NeT45'><ins id='NeT45'></ins><ul id='NeT45'></ul><sub id='NeT45'></sub></form><legend id='NeT45'></legend><bdo id='NeT45'><pre id='NeT45'><center id='NeT45'></center></pre></bdo></b><th id='NeT45'></th></span></q></dt></tr></i><div id='NeT45'><tfoot id='NeT45'></tfoot><dl id='NeT45'><fieldset id='NeT45'></fieldset></dl></div>

                    <small id='NeT45'></small><noframes id='NeT45'>

                    <legend id='NeT45'><style id='NeT45'><dir id='NeT45'><q id='NeT45'></q></dir></style></legend>

                    1. <tfoot id='NeT45'></tfoot>