How to acces values in IConverter

vin111vin111 USMember
edited May 2015 in Xamarin.Forms

My XAML has ListView with ItemSource ="{Binding Data}" and the Iconverter looks like the following. My question : Is it possible to access the value of "Code" in the converter ? All I'm trying to do here is trying to set an icon based on both (ItemType & Code) or ItemType and some other property in "Data"...

            <ListView ItemSource="{Binding Data}">
              <ListView.ItemTemplate>
                <DataTemplate>
                  <ViewCell>
                    <ViewCell.View>
                      <StackLayout Orientation="Horizontal"  VerticalOptions="FillAndExpand">
                        <Image Source="{Binding ItemType, Converter={StaticResource ItemTypeToImageConverter}}" WidthRequest="60"HeightRequest="60"/>
                        <Label Text="{Binding Code}"  VerticalOptions = "Center" HorizontalOptions = "Center"  FontSize="Large" Font="Bold"/>
                      </StackLayout>
                    </ViewCell.View>
                  </ViewCell>
                </DataTemplate>
              </ListView.ItemTemplate>
            </ListView>

public class ItemTypeToImageConverter : IValueConverter
{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value==null)
{
return string.Empty;
}
if (String.IsNullOrEmpty(value.ToString()))
{
return string.Empty;
}
switch (value.ToString())
{
case "01":
{
return "blueIcon";
}
case "02":
{
return "greenIcon";
}
case "03":
{
return "yellowIcon";
}
default:
{
return "redIcon";
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

Best Answer

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Try this:

    <Image Source="{Binding ItemType, Converter={StaticResource ItemTypeToImageConverter}, ConverterParameter={Binding Code}}" WidthRequest="60"HeightRequest="60"/>
    

    Then the Code value will be passed in to the Convert method as the parameter argument.

  • KevinWestheadKevinWesthead GBUniversity ✭✭

    ConverterParameter isn't a BindableProperty so I don't think this will work.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    I guess you're right. That's a shame. Even in WPF it's not bindable so I guess my memory is just bad.

  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    There are few ways to do this.
    1. One way is what @KAW suggested. And it's the easiest.
    2. Another way is you have a 'coerced' value on the view-model. One which combines ItemType and Code.
    3. Similar to 1, you could create something general and reusable, a Behavior which has takes a list of parameters (ItemType and Code) and a converter. Its basically a multi-binding with a multi-value converter.

    Hope this answers your question

  • vin111vin111 USMember

    @KAW Thanks. I had to change var item = value as DataItem; That must be a typo.

Sign In or Register to comment.