C# 将图像添加到wpf
我想向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
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中的第一个子项是什么,。。。