How can I reuse WPF ScrollViewer to create my own scrollable control?

I am trying to improve the graph drawing control that comes with graph #. This is fine, but things get out of hand when you start dragging and dropping nodes. This is my first encounter with WPF, so this is probably a newbie question. :)

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

For this purpose, I'm going to put the GraphCanvas inside the ScrollViewer. It would be pretty easy and simple, if not for one problem. I cannot resize the GraphCanvas itself when the node is dragged outside the bounds, otherwise it will result in an incorrect drag. It's also a problem with the original control (check it out, it comes with the sample app).

It would be nice if I could bind the size / location of the scrollbar to the properties of the GraphCanvas so that the ScrollViewer doesn't physically scroll through anything, but just sets the properties of the GraphCanvas. This, in turn, will do all the actual calculations and scrolling.

How can I do that?

+1


source to share


2 answers


Ok I found it! Three easy steps:

  • Inject System.Windows.Controls.Primitives.IScrollInfo

    into your custom control;
  • Add your custom control to ScrollViewer

    ;
  • Set the property CanContentScroll

    to ScrollViewer

    before True

    .


Voila!

+1


source


Check out this link straight from MSDN. It talks about creating multiple controls in a single composite control:



WPF: Customizing Controls for Windows Presentation Foundation

0


source







All Articles