2012-03-28 190 views
0

我有一个selfcreated usercontrol从画布继承。 现在,这个画布对象总是具有宽度和高度之比,这是非常必要的。 这怎么可能? 我想这一个,但它不工作:画布比例尺寸

 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)); 

    } 

现在我想问一下,如果你有任何想法。该比率是Image.Height/Image.Width(图片为画布对象的背景图像)

回答

0

您可以覆盖ArrangeOverride

protected override Size ArrangeOverride(Size arrangeSize) 
{ 
    Size size = base.ArrangeOverride(arrangeSize); 
    double aspectRatio = ... 

    return new Size(size.Width, size.Width/aspectRatio); 

    // or, if Height determines the final size 
    // return new Size(size.Height * aspectRatio, size.Height); 
} 

而且你必须确保画布家长不同时设置其WidthHeight

例如,您可以将它放在另一个Canvas中,并明确设置您的Canvas'WidthHeight。或者你把它放在网格中并为HorizontalAlignmentVerticalAlignment设置适当的值。

+0

非常感谢这正是我所需要的。所以我想这是最好的编程社区之一 – 2012-03-29 14:11:03