Binding data from JSON object to ListView inside Hub section

大兔子大兔子 提交于 2019-12-12 02:28:48

问题


I was able to successfully bind the data from JSON after Deserialize to listview but i am having trouble binding to the same listview inside a Hub/HubSection.

Using ItemData.ItemsSource = obj; I was able to bind the data to the listview. How can i bind and display the data when listview is placed inside a hubsection?

My MainPage.xaml Hub code

<Hub x:Name="MainHub" Header="My Hub">
        <HubSection x:Name="Test" Header="Online">
            <DataTemplate>
                <Grid>
         <ListView Name="ItemData">
         <ListView.ItemContainerStyle>
         <Style TargetType="ListViewItem">
         <Setter Property="HorizontalContentAlignment" Value="Stretch" />
         </Style>
         </ListView.ItemContainerStyle>
         <ListView.ItemTemplate>
         <DataTemplate>
         <Grid>
         <TextBlock Text="{Binding name}" FontSize="24"/>
         </Grid>
         </DataTemplate>
         </ListView.ItemTemplate>
         </ListView>
                </Grid>
            </DataTemplate>
        </HubSection>
    </Hub>

MainPage.xaml.cs

public async void GetOnline()
    {
        Uri uri = new Uri("http://mywebsite.com");
        Dictionary<string, string> pairs = new Dictionary<string, string>();
        pairs.Add("id", CourseID.ToString());
        HttpClient client = new HttpClient();
        HttpFormUrlEncodedContent formContent = new    HttpFormUrlEncodedContent(pairs);
        HttpResponseMessage response = await client.PostAsync(uri,formContent);
        List<string> CName = new List<string>();
        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            var obj = JsonConvert.DeserializeObject<List<RootObject>>(result);
            for (int i = 0; i < obj.LongCount(); i++)
            {
                CName.Add(obj[i].name);
            }
            //ItemData.ItemsSource = obj;
            //MainHub.DataContext = obj;
        }
    }

    public class RootObject
    {
        public int id { get; set; }
        public string name { get; set; }
    }

Edit 1 :

I was able to retrieve the data now. Edited XAML Code

 <Hub x:Name="MainHub" Header="My Hub">
 <HubSection x:Name="Test" Header="Online">
 <DataTemplate>
 <Grid>
 <ListView Name="ItemData"  ItemsSource="{Binding}"></ListView>
 </Grid>
 </DataTemplate>
 </HubSection>
 </Hub>

Code behind -

 Test.DataContext = CName;

Is this the correct way to bind the data to listview inside hub section?


回答1:


Remove DataTemplate tags of hubsection it should work then.



来源:https://stackoverflow.com/questions/36032815/binding-data-from-json-object-to-listview-inside-hub-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!