【问题标题】:BInding dynamic data to pivot header and pivot itemB将动态数据绑定到数据透视表头和数据透视项
【发布时间】:2016-06-10 09:46:28
【问题描述】:

我正在尝试将数据动态绑定到透视表头和项目。这是我如何绑定数据的编码。

类:

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&sectionName=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 中查看数据。而且我的屏幕越来越空了。

请任何人建议继续进行。

【问题讨论】:

  • 在绑定到 xaml 视图时始终使用 observable 集合。更新源时会更新用户界面
  • @eldho 能否请您在示例中解释如何使用 observable 集合来绑定数据,我以前没有使用过。谢谢。
  • ObservableCollection 集合;这是可观察到的字符串集合,您可以创建 ObservableCollection&lt;DataModel.ArticleDetails&gt; stackoverflow.com/questions/4279185/…

标签: c# xaml windows-phone-8.1


【解决方案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>

【讨论】:

  • 感谢 Chirag Shah。它正在为 pivotitem 工作。但问题出在 PivotHeader 中。我在列表中计数为 5,但在 UI 中仅显示列表中的第一个项目。有没有办法绑定 PivotHeader 中的所有数据。提前谢谢你。
  • 我认为我们无法在 PivotItem 中声明 Header,因为您在代码中进行了编辑。.
  • 哦,对不起!做出了改变。
  • 即使在更改之后我也没有得到标题列表。我在问题中附上了我的输出截图,请查看。在标题列表中,我的计数为 5,但我只能绑定第一项。
【解决方案2】:

我在这里发布了一个非常相似的问题的答案: Working with Pivot

基本上,您必须为 Pivot 控件的 HeaderTemplateItemTemplate 属性指定模板。

【讨论】:

  • 好的,谢谢@Arnaud。让我用我的应用程序检查代码,然后让你知道结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-16
  • 2010-12-28
  • 2023-03-08
  • 1970-01-01
相关资源
最近更新 更多