2012-02-19 8 views
4

Windows Phone 7을 개발 중이므로 ListBox에 이미지를 추가해야합니다.ListBox에 이미지 추가 (C#, Windows phone 7)

XAML이 아닌 C# 코드로 수행하고 싶습니다.

7

내가 XAML 코드가 그것에 대해 읽었지만 모두 내가 윈도우 폰에서 동작하지 않습니다 BitmapImage를 사용하여 내 클래스 MatchDates에서

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

및 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); 

만 빈 공간이 제공됩니다, 어떻게 목록 상자에 이미지를 추가하는 방법은 무엇입니까?

답변

5

하나 :

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 번호 : 나는 WCF 서비스를 사용하는 기사를 검색하기위한

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; 

.

1

BitmapImage가 지원됩니다. BitmapImage를 ImageSource로 선언해야합니다. 내가 사용하는 방법

+0

코드 예를 들어 주시겠습니까? 그것은 내 첫 번째 프로젝트 :) –

+1

나는이 응용 프로그램에서 사용 : Uri imageUri = new Uri ("herecomestheUri", UriKind.RelativeOrAbsolute); 새로운 BitmapImage() {UriSource = imageUri}; ImageSource SrcImage = imageUri; 이것은 아주 간단한 예이지만, 나의 대답 이후의 예도 역시 좋은 예입니다. – MSicc

+0

이것에 대해 뭔가 할 수 있습니까? Uri uri = new Uri ("uri", UriKind.Relative); BitmapImage bitmap = 새 BitmapImage (uri); 이미지 img = new Image(); img.Height = 30; img.Width = 30; img.Source = bitmap; this.Items.Add (img); ? 나는 this.Items.Add (img)가 잘못되었다고 생각한다. –