2014-03-25 75 views
0

列表框這是我的模式,綁定嵌套的JSON在XAML

public class main 
    { 
     public List<categories> categorieslist { get; set; } 
    } 

    public class categories 
    { 
     public int categoryId { get; set; } 
     public string categoryName { get; set; } 
     public List<pdf> pdfdocs { get; set; } 
     public List<video> videoFiles { get; set; } 
    } 

    public class pdf 
    { 
     public string URL { get; set; } 
     public string language { get; set; } 
     public string createdDate { get; set; } 
     public bool isFavorite { get; set; } 
     public bool isRead { get; set; } 
    } 

即時通訊使用JSON.NET反序列化

main mainobj = JsonConvert.DeserializeObject<main>(App.hello); 

我需要爲選定的類別顯示PDF的名單, 林使用LINQ來過濾該特定類別,我無法綁定PDF列表。

pdf.ItemsSource = App.mainobj.categorieslist.Where(i => i.categoryId.Equals(s)); 

    <ListBox x:Name="pdf" Margin="0,0,0,363" ItemsSource="{Binding}" Foreground="White"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding pdf.URL}" Foreground="White"></TextBlock> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
+0

你有類別列表中的對象嗎? – Sajeetharan

+0

mainobj是我創建的唯一對象,它是主類 –

+0

的一個實例什麼是mainobj.categorieslist?它裏面有物品嗎? – Sajeetharan

回答

3
<ListBox ItemsSource="{Binding categorieslist}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding categoryId }" FontSize="20" /> 

       <ItemsControl ItemsSource="{Binding pdf}" Margin="0 20 0 0"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Border BorderBrush="Blue" BorderThickness="2"> 
           <TextBlock Text="{Binding URL }" FontSize="20" /> 
          </Border> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
       <ItemsControl ItemsSource="{Binding videoFiles}" Margin="0 20 0 0"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Border BorderBrush="Red" BorderThickness="2"> 
           <TextBlock Text="{Binding URL}" FontSize="20" /> 
          </Border> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
+0

對不起,這不起作用.. –

+0

我修改了答案!只是檢查 – Sajeetharan

+0

工作正常..非常感謝:) –