Reputation: 3624
I had the following very simple code that, of course, worked :
<Image Source="Resources\data_store.png"/>
The thing is that I now have to take the string from the property ("name") of an object that is given as an ItemSource and I dont manage to get bind to it
I tried these but none work :
<Image Source=name/>
<Image Source=Path="name"/>
<Image Source=Path=name/>
<Image Source={Path="name"}/>
<Image Source={Path=name}/>
<Image Source={Binding name}/>
My object is set in the c# codebehind and I don't think it changes something...
Upvotes: 1
Views: 254
Reputation: 3972
In the code behind, make sure you define the source as Property:
public string Name{get;set;}
then the binding works as follows:
<Image Source="{Binding Name, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
If you want to dynamically update the image, you will need to implement INotifyPropertyChanged and invoke PropertyChanged when the image is changed
Upvotes: 2
Reputation: 70122
The XAML parser is rather clever, when it comes across:
<Image Source="Resources\data_store.png"/>
It knows that the Source
property is of ImageSource
and will use a suitable converter to load the resource specified. If you need to set the image source in code-behind or via a binding you need to do this yourself via a value converter. The following value converter will do the trick:
public classImageUriConverter : IValueConverter
{
publicImageUriConverter()
{
}
public objectConvert(objectvalue, TypetargetType, objectparameter, System.Globalization.CultureInfo culture)
{
Urisource = (Uri)value;
return newBitmapImage(source);
}
public objectConvertBack(objectvalue, TypetargetType, objectparameter, System.Globalization.CultureInfo culture)
{
throw newNotImplementedException();
}
}
Courtesy of this blog post.
Upvotes: 1