M
moondaddy
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.
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.