none
Image source binding with a relative path

    Question

  • I have a list of class instances which contain filenames in string format.

    <ImageName>test1.jpg</ImageName>

    The files are located in the "images" folder inside the "main" folder where the exe file resides.....
    Problem is I cannot display the images inside a listbox. I think it has to do with the relative path...

    <Image source={Binding Path=ImageName}/>

    How can I define the relative path in the binding ? (I could define the full path in the <ImageName> instance, but I do not want to do that as that will result in errors when installing the application on another pc...)

    thanks !
    Sunday, July 26, 2009 10:39 AM

Answers

  • You should use a converter to the path of your file
    public sealed class ImageConverter : IValueConverter
    {
       
    public object Convert(object value, Type targetType,
                             
    object parameter, CultureInfo culture)
       
    {
           
    try
           
    {
               
    return new BitmapImage(new Uri((string)value));
           
    }
           
    catch
           
    {
               
    return new BitmapImage();
           
    }
       
    }

       
    public object ConvertBack(object value, Type targetType,
                                 
    object parameter, CultureInfo culture)
       
    {
           
    throw new NotImplementedException();
       
    }
    }

    In your Window

    <

    Window.Resources>

     

    <local:ImageConverter x:Key="ImageConverter" />

     

     

    </Window.Resources>

    Your XAML

    <

    ListView ItemsSource="{Binding}">

     

    <ListView.ItemTemplate>

     

    <DataTemplate>

     

    <Image Source="{Binding Path=ImagePath, Converter={StaticResource ImageConverter}}" />

     

    </DataTemplate>

     

    </ListView.ItemTemplate>

     

    <ListBox.ItemsPanel>

     

    <ItemsPanelTemplate>

     

    <VirtualizingStackPanel />

     

    </ItemsPanelTemplate>

     

    </ListBox.ItemsPanel>

     

    </ListView>


    Oscar Avarez Guerras - Arquitecto Software en I3B (I+D+I) Blog:http://geeks.ms/blogs/oalvarez Por favor marca como respuesta si te ha ayudado esta respuesta
    • Marked as answer by santee Tuesday, July 28, 2009 8:51 AM
    Sunday, July 26, 2009 4:47 PM

All replies

  • You should use a converter to the path of your file
    public sealed class ImageConverter : IValueConverter
    {
       
    public object Convert(object value, Type targetType,
                             
    object parameter, CultureInfo culture)
       
    {
           
    try
           
    {
               
    return new BitmapImage(new Uri((string)value));
           
    }
           
    catch
           
    {
               
    return new BitmapImage();
           
    }
       
    }

       
    public object ConvertBack(object value, Type targetType,
                                 
    object parameter, CultureInfo culture)
       
    {
           
    throw new NotImplementedException();
       
    }
    }

    In your Window

    <

    Window.Resources>

     

    <local:ImageConverter x:Key="ImageConverter" />

     

     

    </Window.Resources>

    Your XAML

    <

    ListView ItemsSource="{Binding}">

     

    <ListView.ItemTemplate>

     

    <DataTemplate>

     

    <Image Source="{Binding Path=ImagePath, Converter={StaticResource ImageConverter}}" />

     

    </DataTemplate>

     

    </ListView.ItemTemplate>

     

    <ListBox.ItemsPanel>

     

    <ItemsPanelTemplate>

     

    <VirtualizingStackPanel />

     

    </ItemsPanelTemplate>

     

    </ListBox.ItemsPanel>

     

    </ListView>


    Oscar Avarez Guerras - Arquitecto Software en I3B (I+D+I) Blog:http://geeks.ms/blogs/oalvarez Por favor marca como respuesta si te ha ayudado esta respuesta
    • Marked as answer by santee Tuesday, July 28, 2009 8:51 AM
    Sunday, July 26, 2009 4:47 PM
  • How does one go about loading the image file at runtime rather than having it embedded as a resource like most people seem to do?  Even for imagebuttons I would like access the image from the image file in a subfolder of the installation.
    It must be possible to do.  I'm very new to WPF and have been searching for the answer for a few days now.

    Monday, August 17, 2009 2:26 AM