English 中文(简体)
拖动和滚动(WPF)
原标题:Dragging and scrolling (WPF)
  • 时间:2012-05-23 12:36:20
  •  标签:
  • c#
  • wpf

好吧,伙计们,我一直在抓我的头 像疯了这件事情 并花了好几个小时 尝试研究它是如何工作的 但我还没有找到答案。

基本上我所要处理的问题是 我的应用程序里有一棵 文件夹的树景图,即:

Catalog

  Brands
    Nike
    Adidas
    Lactose


  Styles
    Sandles
    Trainers
    Boots

我试图修正的问题是,当我拖动文件夹绕过一个文件夹( 在我的 DragDropManager 类中处理) 时, 我无法向上或向下滚动( 仅仅显示一个可爱的停止符号 ) 。

如果我想把东西从最上移到最下移,这是一个问题。

卷轴本身工作顺利,拖拉不完。

如果有人想看到我守则的任何部分......可以随便问一下......因为我不确定...

我读过好几篇文章,

最佳回答

可能有更好的办法,但我所做的是:

  • 在拖曳目标元素( QueryContinueDrag) 时, 请检查您在控制上的控件是否在其视觉树上有一个滚动查看器 。

  • 现在检查您是否在滚动查看器边缘, 例如 10px away 。

  • 滚动查看器

这里有一些代码:

查找关联的滚动查看器 :

var _scrollViewerControl = FindVisualChild<ScrollViewer>(treeView);

private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
  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;
}

在 QueycontinueDrag 中, 确定当前位置和滚动查看器的三角形 :

// as we don t have eventargs here exposing the current mouse position we use the
// win32 API to get the current mouse position
Win32.POINT p;
if (!Win32.GetCursorPos(out p))
{
    return;
}

//this is the point on the screen
Point point = new Point(p.X, p.Y);

//get position relative to scrollViewerControl
Point controlPoint = _scrollViewerControl.PointFromScreen(point);

if (controlPoint.Y < 10 && -10 < controlPoint.Y)
{
    _scrollViewerControl.LineUp();
}
else if (controlPoint.Y > _scrollViewerControl.ViewportHeight - 10 && _scrollViewerControl.ViewportHeight + 10 > controlPoint.Y)
{
    _scrollViewerControl.LineDown();
}

if (controlPoint.X < 10 && -10 < controlPoint.X)
{
    _scrollViewerControl.LineLeft();
}
else if (controlPoint.X > _scrollViewerControl.ViewportWidth - 10 && _scrollViewerControl.ViewportWidth + 10 > controlPoint.X)
{
    _scrollViewerControl.LineRight();
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签