0
votes

I'm trying to get a test app that grabs data from an online Json file and data bind it after it is deserialized.

I'm receiving the following error for every data bind that I set:

Error: BindingExpression path error: 'field_class_day_value' property not found on 'JSON_Test_2.MainPage+Node, JSON_Test_2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='field_class_day_value' DataItem='JSON_Test_2.MainPage+Node, JSON_Test_2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')

Here is the XAML code

<ListBox x:Name="scheduleList" Margin="10,0,30,0" Height="486" Width="404"
FontSize="20">
    <ListBox.ItemTemplate>
        <DataTemplate >
            <StackPanel Margin="10,0,10,8">
                <TextBlock Text="{Binding field_class_day_value }"
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_class_time_start_value }"
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_class_time_end_value }"
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_class_flex_header_value }" 
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding title }" TextWrapping="Wrap" 
                FontSize="18" />
                <TextBlock Text="{Binding field_class_footer_value }" 
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_class_flex_footer_value }" 
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_class_studio_nid_1 }" 
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_class_instructor_nid }" 
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_ages_value }" 
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_class_header_value }" 
                TextWrapping="Wrap" FontSize="18" />
                <TextBlock Text="{Binding field_class_unique_price_value }" 
                TextWrapping="Wrap" FontSize="18" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Classes that were created for use with the Json:

    public class Node2
    {
        public string field_class_day_value { get; set; }
        public string field_class_time_start_value { get; set; }
        public string field_class_time_end_value { get; set; }
        public string field_class_flex_header_value { get; set; }
        public string title { get; set; }
        public string field_class_footer_value { get; set; }
        public string field_class_flex_footer_value { get; set; }
        public string field_class_studio_nid_1 { get; set; }
        public string field_class_instructor_nid { get; set; }
        public string field_ages_value { get; set; }
        public string field_class_header_value { get; set; }
        public string field_class_unique_price_value { get; set; }
    }

    public class Node
    {
        public Node2 node { get; set; }
    }

    public class RootObject
    {
        public List<Node> nodes { get; set; }
    }

Finally the method that was created to deserialize the JSON file

async private void ReadDataFromWeb()
    {
        var client = new HttpClient(); // Add: using System.Net.Http;
        var response = await client.GetAsync(new
        Uri("url"));
        string result = await response.Content.ReadAsStringAsync();
        RootObject rootobject = JsonConvert.DeserializeObject<RootObject
        (result);

        scheduleList.ItemsSource = rootobject.nodes;
    }

I think I'm receiving the data binding error because there is another object that is in between the rootobject and the actual data that I need to use. Is that what I'm missing or are my data binding statements incorrect?

1

1 Answers

0
votes

Your path is not correct, as the error messages indicates. You're trying to bind to property field_class_day_value but your DataContext in the DataTemplate is node (as your ItemsSource is a list of nodes)

Text="{Binding field_class_day_value}"

It should be

Text="{Binding node.field_class_day_value}"

By the way: why do you wrap your node2 as a property in node?