开发者

WPF C# Image resource in listBox

开发者 https://www.devze.com 2023-04-11 18:40 出处:网络
I am having trouble getting an image, as a resource, to show in a listBox element. It looks like a lot of people are having trouble with it, it seems difficult to do, when to me it sounds like someth

I am having trouble getting an image, as a resource, to show in a listBox element.

It looks like a lot of people are having trouble with it, it seems difficult to do, when to me it sounds like something that would be very common and easy to do.

I've got a file called 'home_icon.jpg' in a folder called 'images' within my resource, so: images/home_icon.jpg.

I've got a list box, for which the full code is shown below:

<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>

I've read a bit online, and I've learned to set the "Build action" for the image to "Resource.

WPF C# Image resource in listBox

Here is the view of my solution explorer, showing the image in a folder:

WPF C# Image resource in listBox

What do I put in the Source="" attribute for the Image on the stackpanel for it to show my image?

I've tried looking at the following resour开发者_如何学Cces and things just aren't sticking:

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

WPF image resources


  1. Build Action should be Content .
  2. Copy To Output Directory should be Copy if newer;
  3. Then absolute or relative paths should work ... images/home_icon.jpg should work or pack://application:,,,/images/home_icon.png should work.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号