在ListBox中添加图片(c#,Windows Phone 7)

4

我正在开发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添加图片?

2个回答

5

One of the ways I used:

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服务来检索文章。


1

支持BitmapImage。您必须将BitmapImage声明为ImageSource。


请问你能给我一个代码示例吗?这是我的第一个项目 :) - Max Zhukov
1
我在一个应用程序中使用了这个:Uri imageUri = new Uri("herecomestheUri", UriKind.RelativeOrAbsolute); new BitmapImage() { UriSource = imageUri }; ImageSource SrcImage = imageUri; 这是一个非常简单的例子,但是我的回答后面那个也是一个很好的例子。 - MSicc
我能对此做些什么吗: Uri uri = new Uri("uri",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(img) 是错误的。 - Max Zhukov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接