我有一个BitmapImage
,想将它设置为Grid
的Background
。这个我试过了
xaml:
<Grid x:Name="ContentPanel">
<Grid.Background>
<ImageBrush x:Name="imgBg" />
</Grid.Background>
</Grid>
c#:
BitmapImage bmp = new BitmapImage();
bmp.DecodePixelWidth =(int) scrnWidth;
bmp.DecodePixelHeight = (int)scrnHeight;
bmp.SetSource(e.ChosenPhoto);
ImageBrush ib = new ImageBrush() { ImageSource = bmp };
imgBg.ImageSource = ib.ImageSource;
输出:输出只是黑色。
问题:使用上面的代码我无法将bitmapimage
设置为Grid
元素的background
,我是不是缺少什么?
更新 我知道,当我们将 image
设置为 background
到 grid
时效果很好,例如:
ImageBrush ib = new ImageBrush() { ImageSource = bmp };
ContentPanel.Background = ib;
但是我需要使用xaml
方式,问题是一样的。
最佳答案
<Grid x:Name="ContentPanel">
<Grid.Background>
<ImageBrush Stretch="None"
ImageSource="Your Path/source"
AlignmentY="Center"
AlignmentX="Center" />
</Grid.Background>
</Grid>
这可能对您有帮助,但不能保证一定会,但请尝试一下。是在这个帖子上找到的。 Here
关于c# - 如何设置网格背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26733089/