我正在尝试动态地将数据绑定到数据透视表头和项目。这是我如何绑定数据的编码。
类别:
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代码:
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 ()
{
}
}
这是我的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>
导致我的Pivot输出
我想将数据从一个列表绑定到标题,将其他数据绑定到PivotItem
。
代码中有什么问题吗?我无法在UI中查看数据。我的屏幕空白了。
请有人建议继续深造。
答案 0 :(得分:1)
执行
pivotSubMenu.DataContext = subMenuList;
而不是
pivotSubMenu.ItemsSource = subMenuList;
透视标题代码:
<PivotItem>
<PivotItem.Header>
<ListView ItemsSource="{Binding TestList}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SMenu}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</PivotItem.Header>
</PivotItem>
答案 1 :(得分:1)
我在这里发布了一个非常类似问题的答案: Working with Pivot
基本上,您必须为 Pivot 控件的 HeaderTemplate 和 ItemTemplate 属性指定模板。