我有一个BitmapImage,并想将其设置Background为Grid。我试过这个
xml:
<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元素,我错过了什么吗?backgroundGrid
更新我知道,当我们设置image为background喜欢时它工作正常grid:
ImageBrush ib = new ImageBrush() { ImageSource = bmp };
ContentPanel.Background = ib;
但我需要使用xaml方式,问题是一样的。