如何在codebehindfile中设置ImageSource

时间:2012-08-10 09:11:40

标签: wpf image code-behind imagesource

我在UserControl中有属性ImageSource。如何将我的代码中的ImageSource设置为我的Resources目录中的Image?

我想将图像源绑定到属性ImageSource

<Image Source="{Binding Path=ImageSource}" />

1 个答案:

答案 0 :(得分:2)

为了从代码中的资源文件创建BitmapImage(从ImageSource派生),您将执行以下操作,前提是文件MyImage.jpg位于名为{{1的文件夹中您的Visual Studio项目,并且其Images设置为Build Action

Resource

另见this answer


您也可以直接使用图像资源而不绑定:

var uri = new Uri("pack://application:,,,/Images/MyImage.jpg"); 
ImageSource = new BitmapImage(uri); // set the ImageSource property
相关问题