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

    <tfoot id='dcYxn'></tfoot>
  1. <small id='dcYxn'></small><noframes id='dcYxn'>

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

        <bdo id='dcYxn'></bdo><ul id='dcYxn'></ul>

      WPF列表框在拖动时自动滚动

      时间:2023-10-08
      <i id='F41Ey'><tr id='F41Ey'><dt id='F41Ey'><q id='F41Ey'><span id='F41Ey'><b id='F41Ey'><form id='F41Ey'><ins id='F41Ey'></ins><ul id='F41Ey'></ul><sub id='F41Ey'></sub></form><legend id='F41Ey'></legend><bdo id='F41Ey'><pre id='F41Ey'><center id='F41Ey'></center></pre></bdo></b><th id='F41Ey'></th></span></q></dt></tr></i><div id='F41Ey'><tfoot id='F41Ey'></tfoot><dl id='F41Ey'><fieldset id='F41Ey'></fieldset></dl></div>
        <bdo id='F41Ey'></bdo><ul id='F41Ey'></ul>

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

                <tfoot id='F41Ey'></tfoot>
              1. <legend id='F41Ey'><style id='F41Ey'><dir id='F41Ey'><q id='F41Ey'></q></dir></style></legend>

                  <tbody id='F41Ey'></tbody>
                本文介绍了WPF列表框在拖动时自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个具有 ListBox 的 WPF 应用程序.拖动机制已经实现,但是当列表太长并且我想将项目移动到不可见的位置时,我不能.

                I have a WPF app that has a ListBox. The drag mechanism is already implemented, but when the list is too long and I want to move an item to a position not visible I can't.

                例如,屏幕显示 10 个项目.我有20个项目.如果我想将最后一个项目拖到第一个位置,我必须拖到顶部并放下.向上滚动并再次拖动.

                For example, the screen shows 10 items. And I have 20 items. If I want to drag the last item to the first position I must drag to the top and drop. Scroll up and drag again.

                如何使 ListBox 自动滚动?

                推荐答案

                知道了.使用了ListBox的事件DragOver,使用了找到的函数here 获取列表框的 scrollviewer ,然后只需稍微调整一下 Position.

                Got it. Used the event DragOver of the ListBox, used the function found here to get the scrollviewer of the listbox and after that its just a bit of juggling with the Position.

                private void ItemsList_DragOver(object sender, System.Windows.DragEventArgs e)
                {
                    ListBox li = sender as ListBox;
                    ScrollViewer sv = FindVisualChild<ScrollViewer>(ItemsList);
                
                    double tolerance = 10;
                    double verticalPos = e.GetPosition(li).Y;
                    double offset = 3;
                
                    if (verticalPos < tolerance) // Top of visible list?
                    {
                        sv.ScrollToVerticalOffset(sv.VerticalOffset - offset); //Scroll up.
                    }
                    else if (verticalPos > li.ActualHeight - tolerance) //Bottom of visible list?
                    {
                        sv.ScrollToVerticalOffset(sv.VerticalOffset + offset); //Scroll down.    
                    }
                }
                
                public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
                {
                    // Search immediate children first (breadth-first)
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                
                        if (child != null && child is childItem)
                            return (childItem)child;
                
                        else
                        {
                            childItem childOfChild = FindVisualChild<childItem>(child);
                
                            if (childOfChild != null)
                                return childOfChild;
                        }
                    }
                
                    return null;
                }
                

                这篇关于WPF列表框在拖动时自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何通过键入在 WPF ListBox 中找到项目? 下一篇:在 XAML 中将可观察集合绑定到 ListBox

                相关文章

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

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