dziwna
dziwna

Reputation: 1242

Loading image from projects files

I'm trying to get png image which is my Resource folder. I tested solution that was written here :Add images to ListBox (c#, windows phone 7). For start I wanted to get the same image for every item in my ListBox. But I can't achive that. The picture doesn't show.

It's how my list in xaml look like:

<ListBox x:Name="ProductList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <Image Source="{Binding ImagePath}"  Stretch="None"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

This is how I populate Items:

List<ImageData1> productsList = new List<ImageData1>();

foreach (ProductItem item in ProductsTable)
{
    if (item.Category == chosenCategory.TrId)
    {
            ImageData1 img = new ImageData1();
            img.Name = item.Name;
            img.ImagePath = new Uri("Resources/img.png", UriKind.RelativeOrAbsolute);
            productsList.Add(img);
    }
}

ProductList.ItemsSource = productsList;

Here is my class to hold image data:

public class ImageData1
{
    public Uri ImagePath
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}

Upvotes: 2

Views: 166

Answers (2)

David Bekham
David Bekham

Reputation: 2175

Probably, You can also use the following assembled code to read the image from the current application.

StreamResourceInfo info =Application.GetResourceStream(new Uri("Resources/img.png", UriKind.RelativeOrAbsolute));
var bitmap = new BitmapImage();
bitmap.SetSource(info.Stream);
img.Soure = bitmap;

Upvotes: 0

Haspemulator
Haspemulator

Reputation: 11308

Try putting / before your image path. Like /Resources/img.png. And try to change UriKind.RelativeOrAbsolute to UriKind.Relative. And make sure your images are added as Content (Build Action property of image). That way it works for me.

Upvotes: 2

Related Questions