2016-06-10 2 views
0

피벗 컨트롤에 피벗 헤더에 대한 목록과 피벗 항목에 대한 목록이 각각 2 개씩 있습니다. 나는 그처럼 구속력이 없습니다.피벗을 사용한 작업

아래는 .cs 부분입니다.

public class ViewModel 
{ 
    List<Feed> feeds = new List<Feed>(); 

    public ViewModel() 
    { 
     GetArticlesListingData(); 
    } 

    public List<Feed> Feeds { get { return this.feeds; } } 

    private async Task GetArticlesListingData() 
    { 
     try 
     { 
      for (var k = 0; k < 6; k++) 
      { 
       Feed feed1 = new Feed(); 
       feed1.Title = "National" + k; 



       HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Get, "FEEDURL"); 
       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(); 

       List<Article> articleList = new List<Article>(); 
       for (int i = 0; i < mainitemsCount; i++) 
       { 
        string strHeadline = rootObject1[i]["HeadLine"].ToString(); 
        articleList.Add(new Article 
        { 
         Title = rootObject1[i]["Abstract"].ToString(), 
         HeadLine = rootObject1[i]["HeadLine"].ToString() 
        }); 
       } 

       feed1.Articles = articleList; 
       feeds.Add(feed1); 

      } 
     } 
     catch (Exception ex) 
     { 

     } 
    } 
} 


public class Feed 
{ 
    public string Title { get; set; } 
    public List<Article> Articles { get; set; } 
} 

public class Article 
{ 
    public string Title { get; set; } 
    public string HeadLine { get; set; } 
} 

다음은

<Page.Resources> 
    <local:ViewModel x:Key="ViewModel" /> 
    <DataTemplate x:Key="headerTemplate"> 
     <TextBlock Text="{Binding Title}" /> 
    </DataTemplate> 
    <DataTemplate x:Key="pivotTemplate"> 
     <ListView Name="ListBox" ItemsSource="{Binding Articles}"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding Title}" /> 
         <TextBlock Text="{Binding HeadLine}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </DataTemplate> 
</Page.Resources> 

<Pivot DataContext="{StaticResource ViewModel}" x:Name="pivot" HeaderTemplate="{StaticResource headerTemplate}" ItemTemplate="{StaticResource pivotTemplate}" ItemsSource="{Binding Feeds}" ></Pivot> 

이 문제를 해결하기 위해 나를 인도 해주십시오, 내 XAML 부분입니다. 나는 피벗 스타일 여기

enter image description here

+1

당신은 미 XAML 보여줄 수 있습니까? – bit

답변

1

작업 예제가에 이미지 아래로 데이터를 바인딩 할.

먼저, 매우 기본적인 뷰 모델을 만들어 메뉴 항목과 기사를 동시에 저장합니다. 제목 (메뉴 항목)과 기사 목록이있는 피드 목록입니다.

public class ViewModel : BindableBase 
{ 
    ObservableCollection<Feed> feeds = new ObservableCollection<Feed>(); 

    public ViewModel() 
    { 

    } 

    public async Task LoadFeeds() 
    { 
     var myFeeds = new ObservableCollection<Feed>(); 
     Feed feed1 = new Feed(); 
     feed1.Title = "Feed A"; 
     feed1.Articles = new List<Article>() { new Article() { Title = "Article 1", HeadLine = "Headline 1" }, 
     new Article() { Title = "Article 2", HeadLine = "Headline 2"}, 
     new Article() { Title = "Article 3", HeadLine = "Headline 3"}}; 
     myFeeds.Add(feed1); 

     Feed feed2 = new Feed(); 
     feed2.Title = "Feed B"; 
     feed2.Articles = new List<Article>() { new Article() { Title = "Article 4", HeadLine = "Headline 4" } }; 
     myFeeds.Add(feed2); 

     Feed feed3 = new Feed(); 
     feed3.Title = "Feed C"; 
     feed3.Articles = new List<Article>() { new Article() { Title = "Article 5", HeadLine = "Headline 5" }, 
     new Article() { Title = "Article 6", HeadLine = "Headline 6"}}; 
     myFeeds.Add(feed3); 

     Feeds = myFeeds; 
    } 

    public ObservableCollection<Feed> Feeds 
    { 
     get { return this.feeds; } 
     set 
     { 
      Set<ObservableCollection<Feed>>(ref this.feeds, value); 
     } 
    } 
} 

public class Feed 
{ 
    public string Title { get; set; } 
    public List<Article> Articles { get; set; } 
} 

public class Article 
{ 
    public string Title { get; set; } 
    public string HeadLine { get; set; } 
} 

public abstract class BindableBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public virtual bool Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null) 
    { 
     if (object.Equals(storage, value)) 
      return false; 

     storage = value; 
     this.RaisePropertyChanged(propertyName); 
     return true; 
    } 

    public virtual void RaisePropertyChanged([CallerMemberName]string propertyName = null) 
    { 
     if (Windows.ApplicationModel.DesignMode.DesignModeEnabled) 
      return; 

     var handler = PropertyChanged; 
     if (!object.Equals(handler, null)) 
     { 
      var args = new PropertyChangedEventArgs(propertyName); 
      handler.Invoke(this, args); 

     } 
    } 
} 

은보기 코드 숨김에 다음을 추가 : 다음

protected async override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    var viewModel = pivot.DataContext as ViewModel; 
    await viewModel.LoadFeeds(); 
} 

그리고, XAML을 피벗 컨트롤. HeaderTemplateItemTemplate을 지정해야합니다.

<Page 
    x:Class="PivotApp.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:PivotApp" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

    <Page.Resources> 
     <local:ViewModel x:Key="ViewModel" /> 
     <DataTemplate x:Key="headerTemplate"> 
      <TextBlock Text="{Binding Title}" /> 
     </DataTemplate> 
     <DataTemplate x:Key="pivotTemplate"> 
      <ListView Name="ListBox" ItemsSource="{Binding Articles}"> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding Title}" /> 
          <TextBlock Text="{Binding HeadLine}"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
     </DataTemplate> 
    </Page.Resources> 

    <Pivot DataContext="{StaticResource ViewModel}" x:Name="pivot" HeaderTemplate="{StaticResource headerTemplate}" ItemTemplate="{StaticResource pivotTemplate}" ItemsSource="{Binding Feeds}" > 
    </Pivot> 
</Page> 

최종 결과는 다음과 같습니다 enter image description here

+0

@Arnaud Develay 감사합니다. 잘 작동합니다. – Anbarasi

+0

비동기 호출을 사용하여 목록을 가져와 제대로 바인딩하지 않습니다. 내 질문에 pls 모양을 업데이 트하고 tis를 해결하기 위해 안내 – Anbarasi

+0

당신은보기 모델 클래스에'INotifyPropertyChanged' 인터페이스를 구현해야합니다. 그렇지 않으면 초기화 메서드 후에 뷰 모델이 업데이트되었음을 ​​XAML 뷰에 알리지 않습니다. –