【问题标题】:How to binding List<image> to xaml?如何将 List<image> 绑定到 xaml?
【发布时间】:2015-05-12 11:35:42
【问题描述】:

如何将images[1]的宽度和高度为300x300的图像绑定到xaml?

     public class TrackSpotify
    {
        public class ExternalUrls
        {
            public string spotify { get; set; }
        }

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

    public class Item
    {
        public string album_type { get; set; }
        public List<string> available_markets { get; set; }
        public ExternalUrls external_urls { get; set; }
        public string href { get; set; }
        public string id { get; set; }
        public List<Image> images { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string uri { get; set; }
    }

    public class Albums
    {
        public string href { get; set; }
        public List<Item> items { get; set; }
        public int limit { get; set; }
        public string next { get; set; }
        public int offset { get; set; }
        public object previous { get; set; }
        public int total { get; set; }
    }

    public class RootObject
    {
        public Albums albums { get; set; }
    }
}
 "images" : [ {
         "height" : 640,
         "url" : "i.scdn.co/image/8642802d13a53541e313781c34521a0d33099aac",
         "width" : 640
       }, {
         "height" : 300,
         "url" : "..................",
         "width" : 300
       }, {
         "height" : 64,
         "url" : "....................",
         "width" : 64
       } ],

                            <Grid.RowDefinitions>
                                <RowDefinition Height="200"></RowDefinition>
                                <RowDefinition Height="140"></RowDefinition>
                            </Grid.RowDefinitions>
                            <StackPanel  Grid.Row="0">
                                <Image Source="{Binding url}"></Image>
                            </StackPanel>
                            <Grid Grid.Row="1">                                   

                                    <StackPanel VerticalAlignment="Top" Margin="10">
                                        <TextBlock Text="{Binding name}" Foreground="#D4D4D4" FontSize="12"></TextBlock>
                                        <TextBlock Text="{Binding album_type}" Foreground="#404040" FontSize="15" FontWeight="SemiBold"></TextBlock>

                                    </StackPanel>


                            </Grid>
                        </Grid>
                    </DataTemplate>
var arrays = JsonConvert.DeserializeObject<NewReleaseSpotify.RootObject>(query);
            spotifyItems.ItemsSource = arrays.albums.items;

【问题讨论】:

  • spotifyItems 在哪里声明? (用于最后一行代码)您可以尝试使用ItemsControl 来显示图像列表
  • 你有什么问题?从你的一句话中很难说出来。
  • arrays.albums.items 中的每个 Item 可能有多个图像 (List&lt;Image&gt;),因此您可能希望用 ListBox 或其他东西替换单个 Image 控制您的 XAML。无论如何,为了让你开始,试着只显示第一张图片:&lt;Image Source="{Binding images[0].url}"&gt;

标签: c# wpf xaml


【解决方案1】:

我知道这不是真的,因为你得到一个列表而不是接收单个图像,你可以做的是创建一个转换器绑定图像列表或类并返回你想要的图像。尝试首先返回字符串,如果它不起作用(取决于平台)尝试返回一个带有你想要的 url 的新 BitmapImage。

您可以在 BitmapImage 中设置大小(它使处理速度更快),并且也可以在其中设置大小。

【讨论】:

  • 我使用 Json,所以我不知道按列表绑定
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多