爲了簡單起見:我有一個由2個矩形組成的用戶控件。在設計時,控件的用戶設置用戶控件的寬度和用戶控件屬性的其中一個矩形的默認值。我想將默認值作爲百分比,並將其中一個矩形的寬度設置爲其他矩形寬度的百分比。我遇到的困難是我無法獲得外部矩形的寬度來設置其他矩形寬度的百分比(因爲一切似乎都是0或NaN)。下面是一些代碼:如何根據用戶控件上的屬性設置來設置用戶控件中元素的大小?
用戶控制:
<Grid x:Name="LayoutRoot" Background="White">
<Rectangle x:Name="OuterRectangle" Fill="Red"/>
<Rectangle x:Name="InnerRectangle" Fill="Blue"/>
</Grid>
用戶控件後臺代碼:
public partial class ucRectangles : UserControl
{
public Double Percent { get; set; }
public ucRectangles()
{
InitializeComponent();
InnerRectangle.Width = Percent/100 * OuterRectangle.ActualWidth;
}
}
主頁:
<Grid x:Name="LayoutRoot" VerticalAlignment="Center">
<local:ucRectangles Width="400" Height="40" Percent="50"/>
</Grid>
好吧,這工作,但我只需要它來運行一次,在啓動時,而不是每次瀏覽器調整大小或(無論其他)我是否在處理程序本身中分離處理程序。這是好的做法嗎?有什麼影響嗎? – descf 2011-03-31 19:07:54
這很合理,並且沒有任何影響。只需刪除處理程序。 – 2011-03-31 19:09:20