从JSON Response绑定到Listbox的数据

时间:2013-04-12 07:23:52

标签: c# json listbox

这是我第一次做xaml所以请理解我可能会慢慢学习

以下是我的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>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我没有看到属性的集合。 可能你可以做的是收集列表中的属性可能是你的test并把它放在招标中。

或者将Features集合作为列表框的itemsource。 即

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>

希望这会对你有帮助!