2010-09-15 30 views
2

如何在c#中設置網格的背景圖像(後面的代碼)。silverlight grid background

謝謝 Sp 我可以這樣做嗎?

public ImageSource ImageSourcePin 
{ 
    set { this.DreamTypeImagePin.Background = value; } 
} 

這工作,感謝您的幫助

 public String ImageSourcePin 
    { 
     set { 
      ImageBrush img = new ImageBrush(); 
      img.ImageSource = (ImageSource)new ImageSourceConverter().ConvertFromString(value); 
      DreamTypeImagePin.Background = img; 
     } 
    } 
+0

如果您希望XAML進行代碼隱藏轉換。你可以試試這個。 http://xamlt.com/這可能很方便。 – 2010-09-15 18:28:01

回答

4

圖像刷IMG =新的ImageBrush(); img.ImageSource =(ImageSource)new ImageSourceConverter()。ConvertFromString(「Image.jpg」); System.Windows.Controls.Grid g = new System.Windows.Controls.Grid(); g.Background = img;

HTH

11

使用ImageBrush嘗試。把它放在你的<Grid></Grid>標籤之間。

<Grid.Background> 
    <ImageBrush ImageSource="Image.jpg"/> 
</Grid.Background> 

作爲規定,你可以這樣寫:

ImageBrush imgBrush = new ImageBrush(); 
imgBrush.ImageSource = new BitmapImage(new Uri(@"folder\img.jpg", UriKind.Relative)); 
grid.Background = imgBrush;