2013-04-12 109 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