Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Using {Binding .} with value converters

$
0
0

Hi,
I am writing an application using a ListView:

<ListView ItemsSource="{Binding Places}">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <Grid>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <Label TextColor="White" Text="{Binding Name}" Grid.ColumnSpan="2" FontSize="20" />

                <Label TextColor="White" Text="{Binding Path=. , Converter={StaticResource todayConverter}}" Grid.Row="1" />
                <Label TextColor="White" Text="{Binding Path=. , Converter={StaticResource distanceConverter}}" Grid.Row="1" XAlign="End" Grid.Column="1"/>
              </Grid>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>

I need to pass the whole BindingContext object to my converters but in the converters code is value object set to null:

    class DistanceConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                //value is set to null
                if (value == null) Debug.WriteLine("value is null");

                PizzaPlace i = value as PizzaPlace;

                if (i.DistanceTo < 500)
                {
                    return String.Format("{0} (cca {1:0} m)", i.City, i.DistanceTo);
                }
                return String.Format("{0} (cca {1:0.0} km)", i.City, i.DistanceTo / 1000);
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

How can I pass whole BindingContext objects to value converters?


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>