We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind JSON data to xamarin.Forms ListView (SfListView)

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: March 5, 2020 |
Last Revised Date: March 6, 2020

You can bind the data from JSON (JavaScript Object Notation) file in Xamarin.Forms SfListView using the ItemsSource property.

XAML

The JSON data can be bound to the SfListView ItemsSource property.

<syncfusion:SfListView x:Name="listView" 
                                     ItemSize="70" 
                                         ItemsSource="{Binding ItemsSource}">
            <syncfusion:SfListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Label Grid.Column="0" Text="{Binding Converter={StaticResource converter}, ConverterParameter=Value1}" HorizontalOptions="Start" TextColor="Black" FontSize="16" FontAttributes="Bold"/>
                            <Label Grid.Column="1" Text="{Binding Converter={StaticResource converter}, ConverterParameter=Value2}" HorizontalOptions="Start" TextColor="Black" FontSize="16" FontAttributes="Bold"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView>

C#

Accessed the JSON file from local folder and StreamReader reads the data to return as a dynamic object.

public ViewModel()
{
    var assembly = typeof(MainPage).GetTypeInfo().Assembly;
    Stream stream = assembly.GetManifestResourceStream("ListViewXamarin.Data.Data.json");
    using (StreamReader sr = new StreamReader(stream))
    {
        var jsonText = sr.ReadToEnd();
        ItemsSource = JsonConvert.DeserializeObject<dynamic>(jsonText);
    }
}

 

C#

Dynamic object value converted by ExpandoObject to show the values.

public class DynamicToPathValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return value;
        ExpandoObject busniessObject = JsonConvert.DeserializeObject<ExpandoObject>(value.ToString());
        var jsonList = busniessObject.ToList();
        if (parameter.Equals("Value1"))
            return jsonList[0].Value;
        if (parameter.Equals("Value2"))
            return jsonList[1].Value;
        return value;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

Download the sample from GitHub.

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile