wpf - 通过绑定(bind) -WPF 从资源中显示图像

标签 wpf image resources

我在资源上有一个图标,它的关键是:xxx
我想将它绑定(bind)到 xaml 中的图像..

1:

  <Image Source="{x:Static p:Resources.xxx}"></Image>

2:
  <Image>
     <Image.Source>
         <BitmapImage UriSource="{Binding x:Static p:Resources.xxx}"/>
        </Image.Source>
   </Image>

3:
 <Image Source=" {Binding x:Static p:Resources.xxx,Converter={StaticResource IconToBitmap_Converter}}"></Image>

4:
 <Image>
    <Image.Source>
        <BitmapImage UriSource="{Binding x:Static p:Resources.xxx,Converter={StaticResource IconToBitmap_Converter}}"/>
    </Image.Source>
 </Image>

以上方法都行不通,我该怎么做呢?

最佳答案

首先,您必须将您的图像添加到解决方案资源管理器中的资源文件中。接下来,您必须将 Image 的 Build Action 设置为 Resource,然后您可以在 XAML 代码中使用它,如下所示:

<UserControl>
<UserControl.Resources>
    <ResourceDictionary>
        <BitmapImage x:Key="name" UriSource="Resources/yourimage.bmp" />
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <Image  Source="{StaticResource name}"/>
</Grid>
</UserControl>

关于wpf - 通过绑定(bind) -WPF 从资源中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751525/

相关文章:

amazon-web-services - 您如何动态授予 AWS Lambda 函数访问资源的权限?

wpf - 内容更改时 DataGridView 样式不更新

wpf - Visual Studio 2010 属性帮助

image - 在 Wordpress 的主页上显示随机帖子附件

Android 将文本转换为位图(包括字体、颜色和大小)的最佳方法是什么?

c# - C# 中的图像转换

wpf - 为什么这个多余的空间会出现在网格中?

html - canvas.getContext ("2d") 未定义

python - 用于在 python 中进行词法分析、标记化和解析的资源

android - 将浮点值添加到 android 资源/值