[英]BInding dynamic data to pivot header and pivot item
I am trying to bind data dynamically to pivot header and item. 我正在尝试将数据动态绑定到数据头和项目。 Here is the coding how i am binding the data. 这是我如何绑定数据的编码。
Class: 类:
public class SubMenu
{
public SubMenu()
{
TestList = new List<SubMenu>();
ArticleDetails = new List<ArticleDetails>();
}
public string SectionId { get; set; }
public string ParentSectionId { get; set; }
public string TitleofAccess { get; set; }
public string SMenu { get; set; }
public string Headline { get; set; }
public List<ArticleDetails> ArticleDetails { get; set; }
public List<SubMenu> TestList { get; set; }
}
XAML.cs code: XAML.cs代码:
private async Task GetArticlesListingData()
{
try
{
var subMenuList = new DataModel.SubMenu();
List<DataModel.ArticleDetails> detail = new List<DataModel.ArticleDetails>();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Constants.Constants.URL.LiveUrl + "/api/Menu/GetSubMenuList?parentSectionId=1");
request.Headers.Add("UserAgent", "Windows 8 app client");
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
var result = await response.Content.ReadAsStringAsync();
var rootObject = JArray.Parse(result);
for (var i = 0; i < rootObject.Count; i++)
{
subMenuList.TestList.Add(new DataModel.SubMenu()
{
SMenu = rootObject[i]["Title"].ToString(),
SectionId = rootObject[i]["SectionId"].ToString()
});
}
HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Get, "http://52.70.18.77:84/api/News/NewsHome/Listing?recordCount=20§ionName=National&districtName=chennai");
request1.Headers.Add("UserAgent", "Windows 8 app client");
request1.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
request1.Headers.Add("Authorization", "bearer " + accessToken);
HttpClient client1 = new HttpClient();
HttpResponseMessage response1 = await client1.SendAsync(request1, HttpCompletionOption.ResponseHeadersRead);
var result1 = await response1.Content.ReadAsStringAsync();
result1 = Regex.Replace(result1, "<[^>]+>", string.Empty);
var rootObject1 = JArray.Parse(result1);
int mainitemsCount = rootObject1.Count();
for (int i = 0; i < mainitemsCount; i++)
{
subMenuList.ArticleDetails.Add(new DataModel.ArticleDetails
{
Articleid = rootObject1[i]["ArticleId"].ToString(),
Abstract = rootObject1[i]["Abstract"].ToString(),
URL = rootObject1[i]["Url"].ToString(),
HeadLine = rootObject1[i]["HeadLine"].ToString(),
});
}
pivotSubMenu.ItemsSource = subMenuList;
}
catch ()
{
}
}
And here is my XAML code where I am trying to bind the data: 这是我尝试绑定数据的XAML代码:
<Pivot x:Uid="Pivot" x:Name="pivotSubMenu" CommonNavigationTransitionInfo.IsStaggerElement="True"
SelectionChanged="pivotSubMenu_SelectionChanged">
<Pivot.HeaderTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding TestList}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SMenu}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</Pivot.HeaderTemplate>
<PivotItem>
<ListView Background="Brown" ItemsSource="{Binding ArticleDetails}"
ItemTemplate="{StaticResource ArticlesLisitngTemplate}" Margin="-17 0 -15 0">
</ListView>
</PivotItem>
</Pivot>
resulting in the output of my Pivot 导致我的数据透视输出
I want to bind the data from one list to header and other to PivotItem
. 我想将数据从一个列表绑定到标头,将另一个绑定到PivotItem
。 Is there anything wrong in the code. 代码有什么问题吗? I am not able to view data in the UI. 我无法在用户界面中查看数据。 And I am getting empty screen. 而且我的屏幕空了。
Please anyone suggest to proceed further. 请任何人建议继续进行。
Do 做
pivotSubMenu.DataContext = subMenuList;
instead of 代替
pivotSubMenu.ItemsSource = subMenuList;
Pivot header code: 枢轴标头代码:
<PivotItem>
<PivotItem.Header>
<ListView ItemsSource="{Binding TestList}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SMenu}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</PivotItem.Header>
</PivotItem>
I have posted an answer to a very similar issue here: Working with Pivot 我在这里发布了一个非常相似的问题的答案: 使用Pivot
Basically, you have to specify a template for HeaderTemplate and ItemTemplate attributes of your Pivot control. 基本上,您必须为Pivot控件的HeaderTemplate和ItemTemplate属性指定一个模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.