Ross Dargan
Ross Dargan

Reputation: 6021

Binding a Bitmap Image Source to an image in my Local folder

I have a list of images in my ApplicationData.Current.LocalFolder folder. I want to display the first image in an Image control.

On my viewmodel class I have the following code:-

StorageFolder folder = ApplicationData.Current.LocalFolder;
IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
if (files.Count > 0)
{
    vm.SelectedImage = files[0].Name;
}

and my Xaml has the following code:

<Image>
       <Image.Source>
           <BitmapImage UriSource="{Binding SelectedImage, Mode=OneWay}" CreateOptions="BackgroundCreation"/>
        </Image.Source>
</Image>

But I can't figure out the correct string to pass in to get the image to show - any help would be appreciated!

Ta

Ross

Upvotes: 2

Views: 3936

Answers (1)

JustinAngel
JustinAngel

Reputation: 16092

The simplest way to get IsoStore databinding to work is to databind Image.Source to the Path property, not the Name property.

private async void SetImage()
{
    var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
    this.DataContext = files.First();
}

XAML Databinding:

 <Image x:Name="img" Source="{Binding Path}" Width="100" Height="100" />

And here's a print screen of Image.Source showing up as StorageFile.Path:

Image.Source showing up to StorageFile.Path

Upvotes: 9

Related Questions