How to change the background of a grid to an image in WP7 silverlight?

狂风中的少年 提交于 2019-12-08 01:48:50

问题


I'm trying to set a background for a grid control in WP7 silverlight, I need to do that programatically, not in the desighn.

I tried something like:

ContentPanel.Background = new BitmapImage(new Uri("Images\Backgrounds\with box\13.jpg", UriKind.Relative));

But of course, I got an error, because on the right hand we should have the type SolidColorBrush.

is there a way to do that?

Thanks..


回答1:


I would firstly recommend that you use a Canvas instead of a Grid. You can do this by deleting the Grid and inserting a Canvas through the Toolbox -> Drag and Drop.

Then, you can use the code as simple as:

ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("url", UriKind.Relative));
CanvasName.Background = imageBrush;

That will change the background to whatever you want.

Hope that helps.



来源:https://stackoverflow.com/questions/10949571/how-to-change-the-background-of-a-grid-to-an-image-in-wp7-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!