-1

I have a hard coded Image Source defined like this (the image is located in a file):

<Image Source="C:\Work\my_image.png"/>

The image is shown, all works. But now I would instead like to bind this Image Source. I tried like this:

<Image Source="{Binding ImageFilename}"/>

And

public class DataStuff
{
    public string ImageFilename { get; set; }

    public DataStuff(string imageFilename)
    {
        ImageFilename = imageFilename;
    }
}

And

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new DataStuff(@"C:\Work\my_image.png");
    }

But the image is not shown.

What am I missing?

Eerik
  • 49
  • 5

1 Answers1

-1

Solved :-)

public class DataStuff
{
    public ImageSource ImageFilename { get; set; }

    public DataStuff(string imageFilename)
    {
        ImageFilename = new BitmapImage(new Uri(imageFilename, UriKind.Absolute));
    }
}
Eerik
  • 49
  • 5
  • Due to built-in type conversion it should as well work with a filename string. You may however need to assign the DataContext before calling InitializeComponent, or fire a property change notification event when ImageFilename changes. – Clemens Nov 09 '17 at 09:38