我无法将图像作为资源显示在listBox元素中。WPF C#列表框中的图像资源
看起来很多人都遇到了麻烦,这似乎很难做到,对我来说这听起来像是一件非常常见和容易做到的事情。
我在我的资源中名为'images'的文件夹中有一个名为'home_icon.jpg'的文件,所以:images/home_icon.jpg
。
我有一个列表框,用于其全部代码如下所示:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Height="253" HorizontalAlignment="Left" Margin="10,46,0,0" Name="listBox1" VerticalAlignment="Top" Width="481">
<ListBoxItem>
<StackPanel Orientation="Horizontal" >
<Image Source="" Width="50" Opacity="50" />
<TextBlock Margin="0,5,0,0" Text="13 Scale Hall Lane" Width="425" />
</StackPanel>
</ListBoxItem>
</ListBox>
<Label Content="Welcome to the house manager" Height="28" HorizontalAlignment="Left" Margin="14,12,0,0" Name="label1" VerticalAlignment="Top" />
</Grid>
</Window>
我读了一下网上,我已经学会了设置为“构建行动” 。图像“资源
这是我的解决方案管理器的图,示出文件夹中的图像:
为了显示我的图像,我在Stackpanel上为图像添加了Source =“”属性的内容?
我试着看下面的资源,事情就不会坚持:
http://msdn.microsoft.com/en-us/library/aa970069.aspx#The_Pack_URI_Scheme
http://channel9.msdn.com/forums/TechOff/416423-WPF--How-to-load-image-from-resource
你有什么试过的?是否Source =“images/home_icon.jpg”不起作用?如果它没有检查它是否在同一目录中,那可能是一个问题。在我的情况下,我使用资源文件夹,所以我必须这样做:Source = ../../Images/home_icon.jpg – MBen