好吧,我在我的项目资源约5 Images
。我想要做的是绑定我的项目资源中的Image.Source
。我的C#代码很容易,我只是这样做:WPF从项目资源绑定图像源
ImageHolder.Source = Propetries.Resources.Image1.png
。
这怎么能在XAML中完成? 类似这样的:
<Image Source={??????}/>
在此先感谢。
好吧,我在我的项目资源约5 Images
。我想要做的是绑定我的项目资源中的Image.Source
。我的C#代码很容易,我只是这样做:WPF从项目资源绑定图像源
ImageHolder.Source = Propetries.Resources.Image1.png
。
这怎么能在XAML中完成? 类似这样的:
<Image Source={??????}/>
在此先感谢。
确保您Build Action
用于image
被标记为Resource
,然后你可以简单地做到这一点在你的XAML -
<Image Source="Properties/Resources/a.png"/>
假设Propetries/Resources
是文件夹结构,在您的项目在您的形象出现。
当您将图像添加到resx文件时,Visual Studio将创建Resources文件夹并将其放入图像文件。
为了在绑定中使用该图片,您需要将构建操作从None更改为Resource。之后,你可以绑定如下:
<Image Source="Resources/your_image_name.png"/>
不能直接绑定到Propetries.Resources.your_image_name因为你需要System.Drawing.Bitmap转换为WPF的BitmapSource。但是你可以绑定到字符串在Resource.resx:
<TextBlock Text="{x:Static properties:Resources.YourStringResource}"></TextBlock>
这里了解如何System.Darwing.Bitmap转换为WPF位图:Load a WPF BitmapImage from a System.Drawing.Bitmap
这里大约在RESX文件绑定到值:Get values from *.resx files in XAML
Propetries/Resources *应该是*属性/资源 – Drammy
@Drammy - 这只是我猜测OP的一个错字。无论如何更新了答案。 –