0

this is my xaml

<Window.Resources>
        <DataTemplate x:Key="listBoxTemplate">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding thumb}" Height="100" Width="130" Margin="5"></Image>
                <StackPanel Orientation="Vertical" Width="247">
                    <TextBlock Text="{Binding recipeName}" Height="60" Padding="15" FontSize="16" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
                    <TextBlock Text="{Binding cuisine}" Height="60" Padding="15" FontSize="16" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

and my code:

conn = new SQLiteConnection(connString);
                cmd = new SQLiteCommand();
                dtSet = new DataSet();

                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT id,recipeName,cuisine,thumb FROM RECIPES";

 dataAdapter = new SQLiteDataAdapter();
            dataAdapter.SelectCommand = cmd;

            try{
                dataAdapter.Fill(dtSet,"recipes");
                listBox1.DataContext = dtSet;

            }

the problem is the image doesn't appear. let's say the thumb returned from sqlite is the image file name 1.jpg, 2.jpg and so on.. so where do I put the images to make it appear in the program? in which folder?

i'm having the exact problem with WPF Image Source binding with StringFormat and https://stackoverflow.com/questions/3717968/button-image-source-binding-using-string-format-in-wpf and the answers are I need to make some kind of converter that will convert the image path into bitmap source.. but being a noob I don't really know how to do it.

Community
  • 1
  • 1
imin
  • 4,504
  • 13
  • 56
  • 103

2 Answers2

0

Normally it should just be the root folder of your application.

If it's not working for you, I'd suggest using the tools within Visual Studio to figure out what the source string should be. Go to the XAML design view, and view the properties of your image control. There you should see a "..." button next to the source property, which lets you select the image. When I do this, it gives me a path like this: */MyProject;component/Images/V__6C41.jpg*

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • ok now if i got the path already, where should i put the path? in the code or xaml? – imin May 21 '12 at 03:55
  • @imin I suppose you could be either, but in the XAML seems like the best place. Use **StringFormat** to bind the correct path, like {Binding Path=thumb,StringFormat=images/{0}} if the path is *images/1.jpg* – McGarnagle May 21 '12 at 03:58
  • hmmm.. i've changed the xaml to but it still display nothing.. – imin May 21 '12 at 04:14
  • I'm out of ideas :(. All I can say is hard code the image path until it works; and display your source binding in a text box so you can confirm it's correct. – McGarnagle May 21 '12 at 04:21
  • thanks for your suggestion, i tried to hard code the image but when i build the apps i returned an error "Cannot locate resource 'images/1.jpg'".. but when I changed it to point to another image which is listed in the Images folder at the solution explorer, the image is being displayed. but i already copied the image 1.jpg to the images folder using windows explorer, but it is not being listed at the solution explorer.. why? – imin May 21 '12 at 04:30
  • @imin Ah, ok I think I see now ... it's not enough to add using Windows Explorer, you actually have to add it to the VS project. Drag and drop it from Windows Exp. into Visual Studio, or right-click on the folder in VS and click *Add->Existing Item* – McGarnagle May 21 '12 at 04:33
  • huhu still don't work. perhaps i need to clarify that the part I said the image is being displayed when I hard code it, i write – imin May 21 '12 at 05:21
  • VS returns System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value '1.jpg' (type 'String'); fallback value will be used, if available. BindingExpression:Path=thumb; DataItem='DataRowView' (HashCode=37296927); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') IOException:'System.IO.IOException: Cannot locate resource '1.jpg'. – imin May 21 '12 at 05:21
0

it seems i need to add a converter. pls refer to my question (and answered!) here the type imageconverter was not found

Community
  • 1
  • 1
imin
  • 4,504
  • 13
  • 56
  • 103