通过绑定 -WPF 从资源中显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14751525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:40:35  来源:igfitidea点击:

Display image from resources by binding -WPF

wpfimageresources

提问by Hodaya Shalom

I have an icon on resources that it key is: xxx

我在资源上有一个图标,它的关键是: xxx

I want to bind it to an image in xaml..

我想将它绑定到 xaml 中的图像..

1:

1:

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

2:

2:

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

3:

3:

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

4:

4:

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

The above ways does not work, how am I supposed to do that?

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

回答by user2025830

First you must add your Image into a Resource File in the Solution Explorer. Next you must set the Build Action of your Image to Resource and then you can use it in the XAML Code like this:

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

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

回答by Dwight

First: Add resources rsx then: add images as images to the resource file and set the image build action to Resource. Now you can access the images like this:

首先:添加资源 rsx 然后:将图像作为图像添加到资源文件中,并将图像构建操作设置为 Resource。现在您可以像这样访问图像:

<Image Source="pack://application:,,,/Resources/image.png"/>