PWL
PWL

Reputation: 466

Binding Image Source vs set Image Source with string

I'm having trouble understanding why using strings in XAML to bind to images in the assets folder works, and using strings to set the Image.Source in C# does not work.

As an example in the SampleData.json there is an ImagePath. In XAML, by default in the GridApp Template, you just bind it like Source="{Binding ImagePath}" and it's working fine.

// SampleData.json
...
"UniqueId": "Group-1",
"Title": "Group Title: 1",
"Subtitle": "Group subtitle: 1",
"ImagePath": "Assets/DarkGray.png"
...

But if I want to set the Image.Source I need to convert it to a BitmapImage or similar. Why?

Another question related to this is if I change Assets/DarkGray.png to C:/a.png it no longer show up. Is there no way to bind to images outside the project-folders?

Upvotes: 1

Views: 400

Answers (1)

Tamás Deme
Tamás Deme

Reputation: 2228

To answer the first part:

Xaml sometimes uses TypeConverters to convert values and figure things out for you. A good example for this is the Foreground property. You can say Foreground="Red", and the system figures out that you meant new SolidColorBrush(Colors.Red). If you do the same from code these converters aren't used and therefore it won't work.

The second part:

Every Windows Store app runs in it's own sandboxed environment. You can't access anything outside of it, except with some APIs. (file pickers, media library etc... - but most of these require user interaction.

Upvotes: 1

Related Questions