我在 UserControl 中有 Property ImageSource。如何ImageSource在我的资源目录中将我的代码中的图像设置为图像?
我想将图像源绑定到属性ImageSource。
<Image Source="{Binding Path=ImageSource}" />
我在 UserControl 中有 Property ImageSource。如何ImageSource在我的资源目录中将我的代码中的图像设置为图像?
我想将图像源绑定到属性ImageSource。
<Image Source="{Binding Path=ImageSource}" />
为了从代码中的资源文件创建BitmapImage(从 ImageSource 派生),您将执行以下操作,前提是该文件MyImage.jpg位于以Images您的 Visual Studio 项目命名的文件夹中,并且其Build Action设置为Resource:
var uri = new Uri("pack://application:,,,/Images/MyImage.jpg");
ImageSource = new BitmapImage(uri); // set the ImageSource property
另请参阅此答案。
您也可以不绑定直接使用图像资源:
<Image Source="/Images/MyImage.jpg" />