发布与ListBox WP7的绑定

时间:2012-04-16 18:57:46

标签: c# windows-phone-7 xaml

我尝试使用hub tile和两个文本块在列表框中显示三个不同的值。现在只出现三个项目中的两个。版本url和发布name以及第三项artistName未显示。我正在使用webclient下载JSON数据。这是我目前的设置。

首先是我的课程

public class NewReleasesCharts 
{
    //public Metadata metadata { get; set; }
    public ResultHome results = new ResultHome();
    public IEnumerator<ResultHome> GetEnumerator()
    {
        return this.results.GetEnumerator();
    }
}

public class ResultHome
{
    public List<FeaturedReleases> featuredReleases { get; set; }

    //public List<FeaturedCharts> featuredCharts { get; set; }
    //public List<TopDownloads> topdownloads { get; set; }
    //public List<MostPopularReleases> mostPopularReleases { get; set; }
    //public List<Components> components { get; set; }

    internal IEnumerator<ResultHome> GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

public class FeaturedReleases
{
    public int id { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public List<ReleaseArtist> artists { get; set; }
    public ReleaseImage images { get; set; }
}

public class ReleaseArtist
{
    public int artistID { get; set; }
    public string artistName { get; set; }
}

public class ReleaseImage
{
    //public ReleaseSmall small { get; set; }
    public ReleaseMedium medium { get; set; }
    public ReleaseLarge large { get; set; }
}

public class ReleaseMedium
{
    public int width { get; set; }
    public int height { get; set; }
    public string url { get; set; }
    public string secureUrl { get; set; }
}

public class ReleaseLarge
{
    public int width { get; set; }
    public int height { get; set; }
    public string url { get; set; }
    public string secureUrl { get; set; }
}

XAML

                <ListBox Grid.Row="0" x:Name="listRelease" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <toolkit:HubTile Source="{Binding images.large.url}" Margin="10" />
                                <TextBlock Text="{Binding name}" Width="173" />
                                <TextBlock Text="{Binding artists.artistName}" Width="173" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

和背后的代码

public void jsonHome_GetDataCompleted(object snder, DownloadStringCompletedEventArgs e)
{
    NewReleasesCharts homeData = JsonConvert.DeserializeObject<NewReleasesCharts>(e.Result);

    const int limit = 6;

    this.listRelease.ItemsSource = homeData.results.featuredReleases.Take(limit);
}

可以通过将api:http://api.beatport.com/catalog/3/beatport/home插入JSON formatter来查看JSON字符串。感谢。

更新

第二个列入艺术家的列表框

                <ListBox ItemsSource="{Binding Artists}">
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding artistName}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>    
                </ListBox>

5 个答案:

答案 0 :(得分:1)

输出是否显示错误? 我不认为属性artists.artistName存在,因为它是一个List?

答案 1 :(得分:0)

艺术家是一个清单。您必须将其绑定到列表控件(例如,ListBox或ItemsPanel)。

其他可能性:要仅显示列表中的第一位艺术家,请使用以下语法:

  <TextBlock Text="{Binding artists[0].artistName}" Width="173" />

答案 2 :(得分:0)

ItemSource绑定到Artists时出现大写问题。

<ListBox ItemsSource="{Binding Artists}">
    <ItemsPanelTemplate>
        <toolkit:WrapPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding artistName}" />
        </DataTemplate>
    </ListBox.ItemTemplate>    
</ListBox>

但是,您的FeaturedReleases课程定义artists而非Artists

public class FeaturedReleases
{
    public int id { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public List<ReleaseArtist> artists { get; set; }
    public ReleaseImage images { get; set; }
}

为了与您的静止保持一致,您应该将绑定更改为artists

答案 3 :(得分:0)

尝试使用ObservableCollection,因此应该List<ReleaseArtist> artists { get; set; }而不是公开public ObservableCollection<ReleaseArtist> artists { get; set; },因为对我来说,似乎在您从互联网上收到艺术家之后,UI并未与这些艺术家一起更新

查看本教程:http://msdn.microsoft.com/en-us/library/ms748365.aspx

答案 4 :(得分:-1)

我终于弄清楚了。我将artists绑定到嵌套列表框,并且能够获得我想要的布局。这是代码。

                    <ListBox x:Name="listRelease" Grid.Row="0" >
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <toolkit:HubTile Source="{Binding images.large.url}" Margin="10" IsFrozen="True" />
                                <TextBlock Text="{Binding name}" Width="173" />
                                <ListBox ItemsSource="{Binding artists}" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal" >
                                                <TextBlock Text="{Binding name}" Margin="10,0,0,0"  Width="173" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>