我想我已经理解了MeasureOverrride是如何工作的,但我试图在一个非常简单的情况下使用它,但它不起作用……所以现在我不太确定..。使用测量覆盖后,我是否也必须使用Arrageoverride,否则系统会为我这样做?情况是这样的:我有一个从面板继承的LinearLayout类,它有两个字段叫做wrapwidht和wrapheigh,如果它们是真的,那么LinearLayout的宽度或高度必须是它的子元素所要求的。因此,我的Measureoveride如下所示:
protected override Size MeasureOverride(Size availableSize) {
Size panelDesiredSize = new Size();
if ((this.widthWrap) || (this.heightWrap))
{
foreach (UIElement elemento in this.Children)
{
System.Diagnostics.Debug.WriteLine(((FrameworkElement)elemento).DesiredSize.ToString());
if (this.Orientation.Equals(System.Windows.Controls.Orientation.Vertical))
{
if (this.widthWrap)
{
//the widest element will determine containers width
if (panelDesiredSize.Width < ((FrameworkElement)elemento).Width)
panelDesiredSize.Width = ((FrameworkElement)elemento).Width;
}
//the height of the Layout is determine by the sum of all the elment that it cointains
if (this.heightWrap)
panelDesiredSize.Height += ((FrameworkElement)elemento).Height;
}
else
{
if (this.heightWrap)
{
//The highest will determine the height of the Layout
if (panelDesiredSize.Height < ((FrameworkElement)elemento).Height)
panelDesiredSize.Height = ((FrameworkElement)elemento).Height;
}
//The width of the container is the sum of all the elements widths
if (this.widthWrap)
panelDesiredSize.Width += ((FrameworkElement)elemento).Width;
}
}
}
System.Diagnostics.Debug.WriteLine("desiredsizzeeeeeee" + panelDesiredSize);
return panelDesiredSize;
}我带到LinerLayout的子按钮是3个按钮,但没有绘制任何内容。即使panelDesiredSize字段是正确的..所以也许我不太明白它是如何工作的。如果有人能帮我,我会很高兴的:-)
发布于 2011-04-14 20:37:14
在之前类似于你的帖子上查看我的答案:Two Pass Layout system in WPF and Silverlight
答案是否定的,你没有的来覆盖ArrangeOverride,但是如果你不打算使用MeasureOverride,那么使用ArrangeOverride有什么意义呢?
发布于 2011-04-14 20:35:31
您应该对子对象调用度量方法。请参阅此处的示例:http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.measureoverride.aspx
发布于 2017-04-14 15:30:09
您必须在"elemento“上调用Measure。正是在测量期间,Silverlight创建了在elemento的模板中声明的UI元素,因为需要它们来实际测量并得出所需的大小。然后,您应该使用elemento.DesiredSize.Width和Height为您的panelDesiredSize提供所需的大小。
https://stackoverflow.com/questions/5663209
复制相似问题