💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ 今天帮忙修了一个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); } } } ~~~