C# 发布对ListBox WP7的绑定
我尝试使用中心平铺和两个文本块在一个列表框中显示三个不同的值。目前,三项中只有两项出现。发布url、发布名称和第三项artistName未显示。我正在使用webclient下载JSON数据。这是我目前的设置 我的第一堂课C# 发布对ListBox WP7的绑定,c#,windows-phone-7,xaml,C#,Windows Phone 7,Xaml,我尝试使用中心平铺和两个文本块在一个列表框中显示三个不同的值。目前,三项中只有两项出现。发布url、发布名称和第三项artistName未显示。我正在使用webclient下载JSON数据。这是我目前的设置 我的第一堂课 public class NewReleasesCharts { //public Metadata metadata { get; set; } public ResultHome results = new ResultHome(); public
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
以及背后的代码
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查看JSON字符串:http://api.beatport.com/catalog/3/beatport/home,变成一个。谢谢
更新
绑定到艺术家的第二个列表框
<ListBox ItemsSource="{Binding Artists}">
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding artistName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
输出不显示错误吗?
我认为属性artists.artistName不存在,因为它是一个列表?artists是一个列表。您必须将其绑定到列表控件,例如ListBox或ItemsPanel 其他可能性:要仅显示列表中的第一位艺术家,请使用以下语法:
<TextBlock Text="{Binding artists[0].artistName}" Width="173" />
将ItemSource绑定到美工时存在大写问题 为了保持静止,您应该更改对艺术家的绑定尝试使用,因此不应该使用公共列表艺术家{get;set;}而是应该有公共可观察的集合艺术家{get;set;},因为对我来说,在您从internet接收艺术家后,UI似乎不会随着这些艺术家而更新
查看本教程:我终于能够理解它了。我将艺术家绑定到一个嵌套的列表框,并能够得到我想要的布局。这是密码
<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>
很抱歉,将其绑定到列表控件是什么意思?我可以使用现有的lisbox还是必须创建一个新的lisbox?对不起,所有的问题,我还是新手。你必须用小写字母Artists在你的装订中书写艺术家,直到不起作用为止。你认为我的课有问题吗?@nos9-没有线索。在输出窗口中是否看到任何绑定错误?清理并上传你的源代码树。我来看看。@nos9-是的,这对我没有多大帮助。@nos9-哦,等等。它不是artistName,只是ReleaseArtist类的名称。@nos9-耸肩不起作用是一个相当模糊的问题。这可能是一个或几十个问题。
<ListBox ItemsSource="{Binding Artists}">
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding artistName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
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; }
}
<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>