簡體   English   中英

將圖像添加到ListBox(c#,windows phone 7)

[英]Add images to ListBox (c#, windows phone 7)

我正在開發Windows Phone 7,我需要將圖像添加到ListBox。

我想用C#代碼,而不是XAML。

我讀到了它,但每個人都使用BitmapImage,我無法在Windows Phone 7上工作。

我有XAML代碼:

<StackPanel Margin="0,0,0,17">
    <local:MatchDates Adress="http://soccernet.espn.go.com/results/_/league/esp.1/spanish-la-liga?cc=57393" />
</StackPanel>

和我的班級MatchDates中的C#函數:

    void add_items(List<List<string>> code)
    {
        if (code.Count == 0)
            this.Items.Add("no mathes");
        else
        {
            foreach (List<string> temp1 in code)
            {
                foreach (string temp2 in temp1)
                {
                    this.Items.Add(temp2);
                }
                this.Items.Add("---------------------------------");
            }
        }
    }

如何在add_items函數中添加圖像?

這段代碼:

    Uri uri = new Uri("/eng.png", UriKind.Relative);
    BitmapImage bitmap = new BitmapImage(uri);
    Image img = new Image();
    img.Height = 30;
    img.Width = 30;
    img.Source = bitmap;
    this.Items.Add(img);
    this.Items.Add(temp2);

僅顯示空白空間,如何將圖像添加到ListBox?

我使用的方法之一:

XAML:

<ListBox Name="lstView" ItemsSource="{Binding}">
   <ListBox.ItemTemplate>
      <DataTemplate>
          <StackPanel Orientation="Horizontal">
             <Image Source="{Binding ImagePath}"></Image>
             <TextBlock Text="{Binding Name}"></TextBlock>
        </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

C#:

public class Article
{
    public string Name { get; set; }

    public string ImagePath { get; set; }
}


 Article article1 = new Article() { Name = "name1", ImagePath = "path of image 1" };
 Article article2 = new Article() { Name = "name2", ImagePath = "path of image 2" };

 var articles = new List<Article>();
 articles.Add(article1);
 articles.Add(article2);

 lstView.DataContext = articles;

為了檢索文章,我使用WCF服務。

支持BitmapImage。 您必須將BitmapImage聲明為ImageSource。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM