如何重新使用WPF ScrollViewer中创建自己的滚动控制?自己的、WPF、ScrollViewer

2023-09-06 08:49:39 作者:最爱安然无恙

我想提高图形绘制控件自带的图形#。这是很好的,但事情变得不可收拾时,你周围开始拖动节点。这是我与WPF第一次遇到,所以这可能是一个新手的问​​题。 :)

I'm trying to improve the graph drawing control that comes with Graph#. It's good, but things get out of hand when you start dragging nodes around. This is my first encounter with WPF, so this is probably a newbie question. :)

我有GraphCanvas控制,上有节点和边缘。它们可以拖动围绕而改变它们的坐标,可能使它们为负。我想滚动条添加到控制将允许看到画布上真的有多大。

I have the GraphCanvas control which has nodes and edges on it. They can be dragged around which changes their coordinates, possibly making them negative. I would like to add scrollbars to the control which would allow to see how big the canvas really is.

要为此我在考虑把GraphCanvas内部的ScrollViewer的。这将是pretty的容易和简单,如果不是一个问题。我可能不会调整GraphCanvas自己,当一个节点的边界或本会搞乱拖坏外拖。这也是与原控制(检查出来,它带有一个示例应用程序)。

To this end I'm thinking of putting the GraphCanvas inside a ScrollViewer. Which would be pretty easy and straightforward if not for one problem. I may not resize the GraphCanvas itself when a node is dragged outside the borders or this will mess up dragging bad. That is also the problem with the original control (check it out, it comes with a sample application).

这将是一件好事,如果我可以在滚动条的大小/位置绑定到GraphCanvas的属性,从而使的ScrollViewer不会滚动身体任何东西,只是设置GraphCanvas的属性。这反过来将执行所有实际计算和滚动。

It would be good if I could bind the scrollbar size/location to properties of the GraphCanvas, so that the ScrollViewer would not scroll anything physically, but just set the properties of GraphCanvas. That in turn would perform all actual calculations and scrolling.

如何才能做到这一点?

推荐答案

OK,我找到了!三个简单的步骤:

OK, I found it! Three easy steps:

实施 System.Windows.Controls.Primitives.IScrollInfo 在自定义的控制; 添加自定义控制在的ScrollViewer ; 设置在 CanContentScroll 属性的ScrollViewer 。 Implement System.Windows.Controls.Primitives.IScrollInfo on your custom control; Add your custom control to a ScrollViewer; Set the CanContentScroll property on the ScrollViewer to True.

瞧!