0

I'm trying to set image source with BitmapImage from converted path

MainWindow.xaml

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

            <DataTemplate x:Key="GraphicItem">
            <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Height="24" Width="24" HorizontalAlignment="Left" Source="/Handy Clipboard;component/Resources/image.png" />


            <StackPanel Grid.Column="1">
                <TextBlock Text="{Binding VisibleName}">
                    <TextBlock.ToolTip>
                        <Image Source="{Binding Path=ImagePath, Converter={StaticResource PathToImageConverter}}" />
                    </TextBlock.ToolTip>
                </TextBlock>
            </StackPanel>


        </Grid>
    </DataTemplate>

PathToImageConverter Class

public class PathToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Console.WriteLine(value as string); // "Images\image name.png"
        return new BitmapImage(new Uri(value as string, UriKind.Relative));
    }

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

DataContext of this template is GraphicListItem

public class GraphicListItem : ListItem
{

    public ImageSource Content
    public string ImagePath

    public GraphicListItem() { }
    public GraphicListItem(string visibleName, ImageSource content)
    public GraphicListItem(string visibleName, ImageSource content, string imagePath)
}

I've tried to bind Content and ImagePath with converter but results are the same - empty tooltip (2x2 px rectangle)

Please help, I don't know even why this isn't working

Steve
  • 61
  • 8
  • You usually don't need a converter from path string to ImageSource. The conversion is already done automatically by built-in type conversion. However, you have to make sure that `Images\image name.png` is a valid path. If the image file is loated in a Visual Studio project folder, its Build Action muts be set to Resource, and path should be a [Resource File Pack URI](https://learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf#resource-file-pack-uris). – Clemens Mar 25 '18 at 07:24
  • I agree with you, but when I'm passing path to Source results are the same (empty tool tip). It's valid path, because in other way ImagePath would be null (I was checking if it's bull or empty - it's not). Path is relative, I mean folder Images is in program's directory, photos are not set in as Resource, because they are added and deleted dynamically – Steve Mar 25 '18 at 14:06
  • If image files are added at runtime, they should not be stored in a subfolder of the application executable folder. The application may not have write permission for such a folder. Instead store (and load) the files in an application data folder, e.g. `Environment.SpecialFolder.ApplicationData`. – Clemens Mar 25 '18 at 15:37
  • Okay, after I changed item's Image Path to absolute path, it's started working – Steve Mar 25 '18 at 17:28

0 Answers0