I have a list of data where I keep e.g. countries with code, name and some other data.
List<Country> countries = <deserialized objects from file>
which consist of objects like this:
public class Country
{
public string Code { get; set;}
public string Name { get; set;}
}
The object which use as a DataContext may look like this:
public class Address
{
public string StreetName{ get; set;}
public string CountryCode { get; set;}
}
Then in my XAML I want to do something like this to show the name of the country
<TextBlock Text="{Binding Path=CountryCode, Converter={StaticResource CountryNameLookupConverter}}"/>
But how can I make the CountryNameLookupConverter use the countries list I read from the xml file?
Depending on where you’re exposing the countries collection there are a few different options.
If countries exists in Address or some other ViewModel object you change your converter to implement IMultiValueConverter instead of IValueConverter and then use a MultiBinding to pass both CountryCode and countries (exposed as a property). You would then access and cast values[0] and values[1] to use them to do the lookup in the Convert method.
If you are exposing countries statically (i.e. Lookup.Countries), you can pass the collection to your IValueConverter either as a property or through the ConverterParameter. Here’s the converter with a property:
and the converter resource would be declared like:
Or to pass into Convert’s object parameter instead: