c# - 如何设置网格背景

标签 c# xaml windows-phone-8 bitmapimage imagebrush

我有一个BitmapImage,想将它设置为GridBackground。这个我试过了

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 设置为 backgroundgrid 时效果很好,例如:

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/

相关文章:

css - Windows Phone 8 上显示的电子邮件字体大小问题

c# - 生日年龄计算,也有月数和天数

c# - 在 C#.net 中使重叠的图片框透明

xaml - PowerShell 更改 ScriptBlock 中的变量

.net - XAML -(键入)它有什么意义?

c# - 如何在 Windows Phone 8.1 的 WMAppmanifest 文件中添加语言

c# - 如何在 IE 中使用 asp.net 和 C# 在页面加载时更改图像的 ImageUrl 属性

c# - VS2010 在.Net Framework 版本之间共享项目

c# - Xamarin.Forms 如何将多个页面的 BindingContext 设置为同一个 ViewModel?

xaml - 对于 longlistselector 项目 (WP8),DataTemplate 中突然无效的 XAML