Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c语言中添加网格背景图像#_C#_Wpf_Windows_Wpf Controls - Fatal编程技术网

C# 如何在c语言中添加网格背景图像#

C# 如何在c语言中添加网格背景图像#,c#,wpf,windows,wpf-controls,C#,Wpf,Windows,Wpf Controls,我正在尝试更改c#中的图像源或图像背景,当使用xaml设计UI单击按钮时 <Grid x:Name="gridimage"> <Image x:Name="Image" stretch="Fill"/> </Grid> private void Button1_click(object sender, RoutedEventArgs e) { // NOT WORKING FOR ME

我正在尝试更改c#中的图像源或图像背景,当使用xaml设计UI单击按钮时

<Grid x:Name="gridimage">
            <Image x:Name="Image" stretch="Fill"/>
        </Grid>

private void Button1_click(object sender, RoutedEventArgs e)
    {
     // NOT WORKING FOR ME
         gridimage.Source = new BitmapImage (new Uri("location"));
         gridimage.Background = ?
     }

私有无效按钮1\u单击(对象发送者,路由目标)
{
//不为我工作
Source=新的位图图像(新的Uri(“位置”);
背景=?
}

假设您的Visual Studio项目中有一个
资产
文件夹,其中包含一个图像文件
2.png
,并且该图像文件的
生成操作
设置为
资源
,您将在代码隐藏中创建一个位图图像,如下所示:

var uri = new Uri("pack://application:,,,/Assets/2.png");
var bitmap = new BitmapImage(uri);
然后将BitmapImage用作图像控件的

Image.Source = bitmap;
或者将其与
ImageBrush
一起用作网格的
Background

gridimage.Background = new ImageBrush(bitmap);

你能发布完整的XAML和代码隐藏吗?这与我的答案有什么不同?@Sajeetharan它显示了正确的包URI语法,并显示了如何使用Image或ImageBrush,而不是同时使用两者,这是没有意义的。OP询问如何在代码隐藏中设置Image,我的回答确实有道理sense@Sajeetharan实际上,您没有显示如何在代码隐藏中设置映像,因为(我第三次告诉您)您没有显示包URI。我假设他会处理这个问题,因为我在发布问题时不知道路径。当然,我的答案不值得在这里投反对票。