0

我尝试使用集线器磁贴和两个文本块在列表框中显示三个不同的值。现在只有三个项目中的两个出现。发布url和发布 name和第三项artistName没有出现。我正在使用网络客户端下载 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; }
}

xml

                <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 格式化程序来查看 JSON 字符串。谢谢。

更新

第二个列表框绑定到艺术家

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

5 回答 5

1

输出不是显示错误吗?我认为属性 Artists.artistName 不存在,因为它是一个列表?

于 2012-04-16T19:08:57.993 回答
0

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

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

  <TextBlock Text="{Binding artists[0].artistName}" Width="173" />
于 2012-04-16T19:13:20.130 回答
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

于 2012-04-17T01:33:15.427 回答
0

尝试使用ObservableCollection,因此List<ReleaseArtist> artists { get; set; }应该使用public 而不是 public ,public ObservableCollection<ReleaseArtist> artists { get; set; }因为对我来说,在您从 Internet 接收艺术家之后,UI 似乎没有随这些艺术家更新。

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

于 2012-04-17T20:56:28.313 回答
-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>
于 2012-04-21T22:50:43.817 回答