Reputation: 10713
I have an image in my xaml:
<Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Width="30" Height="30"></Image>
The source for the image is like this:
..\Images\MyFolder\1.png
..\Images\MyFolder\2.png
..\Images\MyFolder\3.png
Basically, the name of the .png file is the id of the items.
In my ViewModel I have a field which represents the id, its name is myId.
How do I add this kind of source - with two hardcoded values and one binding value?
Upvotes: 1
Views: 584
Reputation: 458
Bind the Image source with the id and apply a converter to create the image path.
The xaml will be:
ente<Image Source="{Binding Path=myId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource ConvImageSource}}"/>
And add the resouce at the top:
<Window.Resources>
<local:ImageSourceConverter x:Key="ConvImageSource"/>
</Window.Resources>
And the converter can be:
public class ImageSourceConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
String myId = System.Convert.ToString(value);
String imagePath = SPECIFY_IMAGE_PATH;
String imageExtn = SPECIFY_IMAGE_Extn;
// Create the image source
String imageSource = String.Concat(imagePath, myId, imageExtn);
return imageSource;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
#endregion
}
Hope this will help.
Upvotes: 2