wpf: scroll canvas to objects beyon top or left side.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I have a wpf project where I use a canvas to drag shapes around. when I
drag a shape beyond the right or bottom side I get scrollbars which is good.
I also get scrollbars when I zoom in and a shape goes beyond the right or
bottom side. However, I don't get scrollbars when shapes move beyond the
left or top side of the canvas. This is bad. I need the behavior similar
to Visio where you can drag an object past the left or top and you will get
scrollbars to scroll that area back into view.

I have a grid which has a scrollviewer and the canvas is the content of the
scrollviewer. In my custom canvas control at the end of the mouse move
event I call this line:

this.InvalidateMeasure();

Which causes the next method to run. All it does is re-measures the canvas
area which determines if the right or bottom are out of view. What can I do
here to determine if the top or left are out of view?

protected override Size MeasureOverride(Size constraint)
{
Size size = new Size();
foreach (UIElement element in base.Children)
{
double left = Canvas.GetLeft(element);
double top = Canvas.GetTop(element);
left = double.IsNaN(left) ? 0 : left;
top = double.IsNaN(top) ? 0 : top;

//measure desired size for each child
element.Measure(constraint);

Size desiredSize = element.DesiredSize;
if (!double.IsNaN(desiredSize.Width) &&
!double.IsNaN(desiredSize.Height))
{
size.Width = Math.Max(size.Width, left + desiredSize.Width);
size.Height = Math.Max(size.Height, top + desiredSize.Height);
}
}
// add margin
size.Width += 10;
size.Height += 10;
return size;
}


Note, most of what I do is in c# rather than xaml.

Thanks.


--
moondaddy@xxxxxxxxxxxxxxxx


.



Relevant Pages

  • [Tk] hard resizing
    ... And scrollbars: .ar.sx, .ar.sy ... a window .ar.c.f is created on canvas. ... The .ar frame seems to start with predefined height. ...
    (comp.lang.tcl)
  • Re: Dynamic Class Loading
    ... > To solve your outer problem, I would use a Tk Canvas. ... > It does what you need out of the box: Float a bunch of shapes around the ... oriented roguelike game. ... especially since the class heirarchy in my case ...
    (comp.lang.cpp)
  • scrollbars again
    ... However this canvas has fixed width, which makes me state from the beginning, ... window (scrollbars can remain). ... For each line there is a horizontal bar with some buttons and labels. ...
    (comp.lang.tcl)
  • Re: Scrollbars get obscurred when painting image; how to fix
    ... of the application window so that the scrollbars appear, the image is painted over the scrollbars and obscures them. ... A button of the same size as the Canvas doing the painting does not show the same behavior. ...
    (comp.lang.java.programmer)
  • Re: How can I resolve selecting more than 1 shape for grouping?
    ... You have to insert a drawing canvas (the very bottom item in the ... Insert> Shapes gallery) and put the shapes on the canvas. ... Once all the shapes (and pictures and SmartArt) are on the canvas, ...
    (microsoft.public.word.drawing.graphics)