我想我已經明白了MeasureOverrride是如何工作的,但我試圖用它在一個非常簡單的情況下,它不工作...所以現在我不太確定...使用Measureoverride後,我必須也可以使用Arrageoverride,否則系統會爲我做?情況是這樣的:我有一個從Panel繼承的LinearLayout類,它有兩個稱爲wrapwidht和wrapheigh的字段,如果它們爲true,那麼LinearLayout的寬度或高度必須像其子節點所需的那樣。所以我Measureoveride樣子:你如何在Silverlight中使用MeasureOverride?
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;
}
我aading到LinerLayout兒童是3個按鍵,但沒有被抽..即使panelDesiredSize申請是正確的..所以也許我不明白它是如何工作很好。如果有人可以幫助我會很好:-)
你說得對,但我在ArrangeOverride中編寫了相同的代碼,並且都沒有發生任何事情.. IT假設設置當前元素的大小並調用孩子排列並添加將當前元素添加到樹中?此方法由當前元素的父元素調用,否?當佈局過程?對不起,我讀了很多,但沒有人說誰在調用這個方法......結果是什麼...... – Adriana 2011-04-14 13:27:32