Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binding an image source in XAML

I am trying to bind an image source to my XAML through c#

this works

<Image Source="images/man.jpg"></Image>

this does not work

<Image Source="images/{Binding imagesource}"></Image>

where imagesource is a string variable in the c# file of this xaml and is being set equal to "man.jpg"

like image 534
nooob Avatar asked Jul 29 '10 19:07

nooob


People also ask

How does binding work in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is binding source in WPF?

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property.

What is two way binding XAML?

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.


1 Answers

here is a way how to do it in XAML:

add this to the namespace:

xmlns:System="clr-namespace:System;assembly=mscorlib"

then add your images paths

<System:String x:Key="ImageRefresh">/Theme;component/Images/icon_refresh.png</System:String>
<System:String x:Key="ImageSearch">/Theme;component/Images/icon_search.png</System:String>

This is how you use it

<Image Height="16" Source="{StaticResource ImageSearch}" Stretch="Uniform" Width="16"/>

This works ok, but if you load your xaml style in Blend it will go bogus..

An object of type "System.String" cannot be applied to a property that expects the type "System.Windows.Media.ImageSource".

I haven't figured out yet, how to replace System:String with that Media.ImageSource... but hey.. it works for me in Visual Studio.

like image 66
Rumplin Avatar answered Sep 28 '22 08:09

Rumplin