2013-04-12 112 views
0

這是我第一次做xaml所以請理解,我可能會在學習緩慢從JSON響應數據綁定到Listbox

以下是我的CS代碼。我試圖將「屬性」綁定到列表框。

public DirectionPage() 
    { 
     InitializeComponent(); 

     List<Feature> features = App.dResult.directions[0].features; 
     foreach (Feature f in features) 
     { 
      Attributes a = f.attributes; 
      MessageBox.Show(a.text); 
     } 
    } 

    public ObservableCollection<Feature> test = new ObservableCollection<Feature>(); 

以下是XAML代碼。

<ListBox x:Name="directionListBox" ItemsSource="{Binding}" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <TextBlock Text="{Binding text}" Style="{StaticResource PhoneTextTitle2Style}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

任何幫助將不勝感激。

+0

將您的代碼粘貼爲**文本**,而不是**圖片** .. –

回答

1

我沒有看到屬性的集合。 可能你可以做的是收集列表中的屬性可能是你的test,並把它放在招標。

或將Features集合作爲列表框的項目源。 即

public DirectionPage() 
    { 
     InitializeComponent(); 
     List<Attributes> LAtributes=new List<Attributes>(); 
     List<Feature> features = App.dResult.directions[0].features; 

     foreach (Feature f in features) 
     { 
      Attributes a=new Attributes(); 
      a = f.attributes; 
      LAttributes.add(a); 
      MessageBox.Show(a.text); 
     } 
     directionListBox.ItemsSource=Lattribute; 
    } 

<ListBox x:Name="directionListBox" ItemsSource="{Binding}" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <TextBlock Text="{Binding Path=text}" Style="{StaticResource PhoneTextTitle2Style}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

希望這會幫助你!

+0

對不起,我嘗試過,但仍然無法正常工作。 – qU3st

+0

這個東西是什麼類型的屬性是什麼?正如我看它它也有屬性稱爲文本。 因此,收集第一個屬性與列表,可能如下 列表 attributelist =新列表(); 然後收集您創建的for循環 attributelist.add(f.attributes);和 directionListBox.ItemsSource = attributelist; 畢竟在xaml ItemSource = {綁定路徑文本} //在這種情況下,我正在考慮文本作爲類屬性的屬性。 – ClickBright

+0

目前,我正在嘗試綁定列表框中的屬性並在列表框中顯示「文本」。文本是屬性類 – qU3st