1

I have a converter which implements the IValueConverter interface. Here's the Convert method:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return String.Concat(@"..\Images\myFolder\", System.Convert.ToString(value), ".png");
    }

Here it is in my xaml code, in the resources:

<converters:ImageSourceConverter x:Key="ImageSourceConverter"/>

And here's how I use it:

<Image Source="{Binding Path=myCategoryId, Converter={StaticResource ImageSourceConverter}}" Width="150" Height="150"></Image>

The problem I have is this: I have a few folders with images in my solution. The path for the images is this one:

..\Images\myFolder\1.png
..\Images\myFolderJohn\2.png
..\Images\myFolderJack\3.png

where the name of the file is an id. The code I have in the Convert method works only for the images in myFolder. The name of the folder depends on the user control in which the image is placed. So, it should be something like this:

if (userControl == "John")
    return String.Concat(@"..\Images\myFolderJohn\", System.Convert.ToString(value), ".png");
if (userControl == "Jack")
    return String.Concat(@"..\Images\myFolderJack\", System.Convert.ToString(value), ".png");

How do I know the user control from which the image is shown, in the Convert method?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231
  • This is a typical use case for a `MultiBinding` with a multi-value converter. See e.g. [this answer](http://stackoverflow.com/a/15309844/1136211) for some details. – Clemens Jul 09 '14 at 11:37

2 Answers2

1

You could use a converter parameter, like this:

In the "John" user control:

<Image Source="{Binding Path=myCategoryId, Converter={StaticResource ImageSourceConverter}, ConverterParameter=John}" Width="150" Height="150"></Image>

and in the "Jack" user control:

<Image Source="{Binding Path=myCategoryId, Converter={StaticResource ImageSourceConverter}, ConverterParameter=Jack}" Width="150" Height="150"></Image>
1

Use the ConverterParameter in your binding.

The xaml in John UseControl would be,

<Image Source="{Binding Path=RentalTypeId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource ConvImageSource}, ConverterParameter=John}"/>

And in you converter class use the parameter like this,

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return String.Concat(@"..\Images\myFolder", System.Convert.ToString(parameter), "\\", System.Convert.ToString(value), ".png");
}
Debasis
  • 408
  • 1
  • 3
  • 12