C
C#2y ago
br4kejet

❔ Pan and zoom control zooms into the top left once the content width exceeds the parent width (WPF)

I made a control similar to the view box with a horizontal/vertical offset (for panning) and a zoom scale. The main parts are the arrange and measure functions:
protected override Size MeasureOverride(Size constraint) {
Size size = new Size();
if (this.InternalChild is UIElement child) {
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Size desired = child.DesiredSize;
size.Width = desired.Width * this.ZoomScale;
size.Height = desired.Height * this.ZoomScale;
}
return size;
}
protected override Size ArrangeOverride(Size arrangeSize) {
UIElement child = this.InternalChild;
if (child != null) {
Size desired = child.DesiredSize;
double left = ((arrangeSize.Width - desired.Width) / 2) + this.HorizontalOffset;
double top = ((arrangeSize.Height - desired.Height) / 2) + this.VerticalOffset;
this.InternalTransform = new ScaleTransform(this.ZoomScale, this.ZoomScale, arrangeSize.Width / 2d, arrangeSize.Height / 2d);
child.Arrange(new Rect(new Point(left, top), desired));
}
return arrangeSize;
}
protected override Size MeasureOverride(Size constraint) {
Size size = new Size();
if (this.InternalChild is UIElement child) {
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Size desired = child.DesiredSize;
size.Width = desired.Width * this.ZoomScale;
size.Height = desired.Height * this.ZoomScale;
}
return size;
}
protected override Size ArrangeOverride(Size arrangeSize) {
UIElement child = this.InternalChild;
if (child != null) {
Size desired = child.DesiredSize;
double left = ((arrangeSize.Width - desired.Width) / 2) + this.HorizontalOffset;
double top = ((arrangeSize.Height - desired.Height) / 2) + this.VerticalOffset;
this.InternalTransform = new ScaleTransform(this.ZoomScale, this.ZoomScale, arrangeSize.Width / 2d, arrangeSize.Height / 2d);
child.Arrange(new Rect(new Point(left, top), desired));
}
return arrangeSize;
}
My problem is that when I increase the zoom to the point where the child's visual size (size after scaling) exceeds the size of the parent (in both the width or height), it starts zooming towards the top left corner, whereas normally it zooms towards the center. Anyone know why it's doing this?
2 Replies
br4kejet
br4kejetOP2y ago
Nevermind i fixed it by removing those 3 lines after calling the child's measure function, meaning the arrange function only receives the size of the control itself instead of being affected by the child control's size
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server