2011-04-21 76 views
0

我已經創建了一個用戶控件只是爲了包含一個圖像,因爲我必須使用Measureoverride和arrangeoverride方法,我不能創建一個子類(圖像被seled)...無論如何,事情是,當我調用this.Image.Measure(SizeIwantto給圖像)圖像的desiredSize字段未設置...任何人都知道爲什麼? 我以前曾經管理方法,布點和它的工作... 這裏是代碼(我已經檢查了所有的其他尺寸和他們都不是0或NaN)UserControl繪圖Silverlight 4

protected override Size MeasureOverride(Size availableSize) 
    { 


     //the size that the element wants to have 
     Size imageDesiredSize = new Size(); 

     imageDesiredSize = availableSize; 
     //if the current element's size is a pertentage 
     if ((futureHeight != 0)) 
     { 
      imageDesiredSize.Height = availableSize.Height * futureHeight/100; 
     } 
     if ((futureWidth != 0)) 
     { 
      imageDesiredSize.Width = availableSize.Width * futureWidth/100; 
     } 

     if (widthWrap) 
     { 
      imageDesiredSize.Width = ((BitmapImage)this.Source).PixelWidth; 
     } 
     if (heightWrap) 
     { 
      imageDesiredSize.Height = ((BitmapImage)this.Source).PixelHeight; 
     } 
     System.Diagnostics.Debug.WriteLine("imagedesired" + imageDesiredSize); 
     this.image.Measure(imageDesiredSize); 
     System.Diagnostics.Debug.WriteLine("desiredsize" + this.image.DesiredSize); 

     return imageDesiredSize; 
    } 

回答

0

到底是一個非常簡單的解決方案...在UserControl的構造函數中,我添加了這一行:this.Content = image; 而現在用戶控件的內容被繪製在屏幕上:-)