Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 将图像添加到wpf_C#_Wpf - Fatal编程技术网

C# 将图像添加到wpf

C# 将图像添加到wpf,c#,wpf,C#,Wpf,我想向wpf应用程序动态添加一个图像 我所尝试的: Image img = new Image(); img.Height = 100; img.Width = 100; img.Visibility = Visibility.Visible; Thickness th = new Thickness(100, 100, 0, 0); img.Marg

我想向wpf应用程序动态添加一个图像

我所尝试的:

            Image img = new Image();
            img.Height = 100;
            img.Width = 100;
            img.Visibility = Visibility.Visible;

            Thickness th = new Thickness(100, 100, 0, 0);
            img.Margin = th;

            string strUri2 = String.Format(@"pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
            img.Source = new BitmapImage(new Uri(strUri2));
我知道,除非我将图像添加到内容中,否则图像不会显示

this.Content=img;
但这样一来,应用程序上现有的控件形状就丢失了


现在,我的问题是如何在不丢失应用程序现有控件的情况下将图像添加到内容中。

默认情况下,窗口内容是网格,因此请尝试

(this.Content as Grid).Children.Add(img);

默认情况下,窗口内容是网格,因此请尝试

(this.Content as Grid).Children.Add(img);

在动态加载和显示图像时,仍然需要考虑应用程序的布局。我建议在XAML中添加一个初始为空的图像控件,例如在网格中,然后在代码中设置此控件的源属性

<Grid>
    ... other controls ...
    <Image Name="img" Grid.Row="..." Grid.Column="..."
           Width="100" Height="100" Margin="100,100,0,0"/>
</Grid>

在动态加载和显示图像时,仍然需要考虑应用程序的布局。我建议在XAML中添加一个初始为空的图像控件,例如在网格中,然后在代码中设置此控件的源属性

<Grid>
    ... other controls ...
    <Image Name="img" Grid.Row="..." Grid.Column="..."
           Width="100" Height="100" Margin="100,100,0,0"/>
</Grid>

您应该将其添加到控件容器父项中。这是什么。内容?那么您在可视化树网格stackpanel中的第一个子项是什么…您应该将其添加到控件容器父项中这是什么。内容?那么您在可视化树网格stackpanel中的第一个子项是什么,。。。