【发布时间】:2014-08-06 07:46:34
【问题描述】:
我需要更改放置在 Canvas 上的 Rectangle 大小,所以我继承了 Canvas 类来使用 MeasureOverride:
public class CustomCanvas : Canvas
{
public CustomCanvas() : base(){}
protected override Size MeasureOverride(Size availableSize)
{
Size canvasDesiredSize = new Size();
foreach (UIElement child in Children)
{
child.Measure(new Size(300, 300));
canvasDesiredSize = child.DesiredSize;
}
return canvasDesiredSize;
}
}
并使用它:
<local:CustomCanvas Width="500" Height="800" Background="Gray">
<Rectangle Fill="AliceBlue" Height="100" Width="100"/>
</local:CustomCanvas>
但在执行后,矩形仍然是 100,100 大小(在 XAML 中指定)。当child.Measure(new Size(300, 300)); 执行时child.DesiredSize 显示0,0。那里发生了一些奇怪的事情。
我使用this 作为参考。
【问题讨论】:
-
尽管
child.DesiredSize显示 100,100(如在 XAML 中),但我已尝试在 Panel 控件上执行完全相同的操作,但 Rectangle 根本不可见。
标签: xaml windows-store-apps win-universal-app