【问题标题】:Issue binding to ListBox WP7问题绑定到 ListBox WP7
【发布时间】:2012-04-28 03:04:10
【问题描述】:

我尝试使用中心磁贴和两个文本块在列表框中显示三个不同的值。现在只有三个项目中的两个出现。发布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; }
}

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>

【问题讨论】:

    标签: c# windows-phone-7 xaml


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 抱歉,您将其绑定到列表控件是什么意思?
      • 我可以使用我现有的 lisbox 还是必须创建一个新的?抱歉所有问题,我还是新手。
      • 您必须在绑定中以小写“艺术家”写 Artists
      【解决方案3】:

      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

      【讨论】:

      • 还是不行。你觉得我的课有什么问题吗?
      • @nos9 - 没有线索。您是否在输出窗口中看到任何绑定错误?清理并在某处上传您的源代码树。我去看看。
      • @nos9 - 是的,这对我没什么用。
      • @nos9 - 哦等等。它不是artistName,它只是name 用于ReleaseArtist 类。
      • @nos9- shrug 不起作用是一个非常模糊的问题。可能是一个或几十个问题。
      【解决方案4】:

      尝试使用ObservableCollection,因此应该使用public ObservableCollection&lt;ReleaseArtist&gt; artists { get; set; } 而不是公共List&lt;ReleaseArtist&gt; artists { get; set; },因为对我来说,在您从互联网接收艺术家之后,用户界面似乎没有更新这些艺术家。

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

      【讨论】:

        【解决方案5】:

        我终于明白了。我将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>
        

        【讨论】:

          猜你喜欢
          • 2011-08-14
          • 1970-01-01
          • 2011-06-05
          • 1970-01-01
          • 1970-01-01
          • 2013-05-26
          • 1970-01-01
          • 2011-03-19
          • 2011-07-25
          相关资源
          最近更新 更多