I have a selfcreated usercontrol that inherits from canvas.
Now it is nessesary that this canvas object has always a ratio between width and height.
How is this possible?
I tried this one but it does not work:
private void Canvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
Vector vec = VisualTreeHelper.GetOffset(this);
if(e.WidthChanged)
Arrange(new Rect(vec.X, vec.Y, ActualWidth, Image.Height / Image.Width * this.ActualWidth));
else if(e.HeightChanged)
Arrange(new Rect(vec.X, vec.Y, Image.Width / Image.Height * this.ActualHeight, ActualHeight));
}
Now i would like to ask if you have any ideas. The ratio is Image.Height/Image.Width (Image is the background image of the canvas object)
You could override ArrangeOverride:
And you have to make sure that the Canvas’ parent does not set its
WidthorHeightat the same time.You could for example put it inside another Canvas and set either your Canvas’
WidthorHeightexplicitely. Or you put it in a Grid and set proper values forHorizontalAlignmentandVerticalAlignment.