zoukankan      html  css  js  c++  java
  • TreeView滚动TreeViewItem

     今天帮忙修了一个bug, 在拖动TreeViewItem时,需要滚动TreeView向前翻页,或向后翻页。
    
    思路:
    
    1.找到TreeView控件里的ItemsControl
    
    2.找到ItemsControl里的ScrollViewer
    
    3.判断当前每个Item的高度
    
    4.通过GetCursorPos获取屏幕绝对坐标
    
    5.通过ItemsControl的PointFromScreen把屏幕坐标转为相对于ItemsControl的客户坐标
    6.判断当前坐标是否超出第一Item或者最后一个Item
    7.如果超出第一个Item则向上滚动, 如果超出最后一个Item则向下滚动。

           [StructLayout(LayoutKind.Sequential)]
            public struct POINT
            {
                public int X;
                public int Y;
     
                public POINT(int x, int y)
                {
                    this.X = x;
                    this.Y = y;
                }
     
                public override string ToString()
                {
                    return ("X:" + X + ", Y:" + Y);
                }
            }
     
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool GetCursorPos(out POINT pt);
    

    private void ScrollItemToView(object sender)
            {
                ItemsControl itemsControl = sender as ItemsControl;
                if (null == itemsControl)
                {
                    return;
                }
     
                if (null == this.targetItemContainer)
                {
                    return;
                }
     
                ScrollViewer scv = Utilities.FindChild<ScrollViewer>(itemsControl);
                if (null != scv)
                {
    
                   //每个Item的Container的高度
                    double itemHeight = this.targetItemContainer.ActualHeight;
     
                    POINT currentPosition = new POINT();
                    GetCursorPos(out currentPosition);
     
                    Point relativePos = itemsControl.PointFromScreen(new Point(currentPosition.X, currentPosition.Y));
     
                    if (relativePos.Y >= itemsControl.ActualHeight - itemHeight)
                    {
                        scv.ScrollToVerticalOffset(scv.VerticalOffset + itemHeight);
                    }
                    else if (relativePos.Y <= itemHeight)
                    {
                        scv.ScrollToVerticalOffset(scv.VerticalOffset - itemHeight);
                    }
                }
            }
    
  • 相关阅读:
    全网首发|阿里资深技术专家数仓调优经验分享(上)
    用跨进程子类化技术实现对其它进程消息的拦载
    字符串与16进制互转
    Windows消息前缀
    Delphi 关于钩子函数HOOK (二)
    ACCESS SQL语法参考
    从内存中加载并运行exe
    浅谈Delphi中进程间的数据共享
    字符串排序等算法
    利用内存映射文件在两个进程间共享数据
  • 原文地址:https://www.cnblogs.com/muzizongheng/p/3170891.html
Copyright © 2011-2022 走看看