2012-11-26 3 views
2

모든 ListView 요소 다음에 이미지를 표시하고 싶습니다. 물론 다른 이미지에 요소가 있습니다. 첫 번째 ListView 항목에는 image_1.png가 사용되고, 두 번째 ListView 항목에는 image_2.png 등이 있습니다. 바인딩을해야합니다. 어떻게 구현할 수 있습니까? ?ListView 요소에 이미지를 표시하려면 어떻게해야합니까?

<ListView x:Name="listView"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <!-- IMAGE IS HERE --> 
        </StackPanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
</ListView> 

편집 (1) :

나는 모든 노력을하지만 지금 work.My 코드를하지 않습니다 :

모델 클래스 :

public ImageSource image { get; set; } 

페이지 클래스 :

BitmapImage bi = new 
bi.UriSource = new Uri("ms-appx:///Logos/x.png"); 
c.image = bi; 

XAML 파일 :

<Image Source="{Binding Source=image}"></Image> 

왜 작동하지 않습니까?

답변

1

나는 내 자신의 질문이야 솔루션을 발견 대답 할 수있는 것 같아요

Image image = new Image(); 
image.Source = new BitmapImage(new Uri(@"ms-appx:/Assets/Logos/x.png")); 

<Image Source="{Binding Path=image.Source.UriSource.AbsolutePath}" Width="55" Height="40"/> 
2

당신은이 같은 컬렉션의 항목에 바인딩 할 수 있습니다 :

<Window x:Class="WpfApplication6.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" Name="UI"> 
    <Grid> 
     <ListView ItemsSource="{Binding ElementName=UI,Path=YourCollection}"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Width="200" Height="200"> 
         <Image Source="{Binding Image}"/> 
         <TextBlock Text="{Binding Title}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </Grid> 
</Window> 

가설 클래스/목록

/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    private ObservableCollection<MyListItem> _yourCollection = new ObservableCollection<MyListItem>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     YourCollection.Add(new MyListItem { Title = "Item 1", Image = new BitmapImage(new Uri("C:\\Users\\Dev\\Pictures\\Picture1.PNG", UriKind.RelativeOrAbsolute)) }); 
     YourCollection.Add(new MyListItem { Title = "Item 2", Image = new BitmapImage(new Uri("C:\\Users\\Dev\\Pictures\\Picture2.PNG", UriKind.RelativeOrAbsolute)) }); 
    } 

    public ObservableCollection<MyListItem> YourCollection 
    { 
     get { return _yourCollection; } 
     set { _yourCollection = value; } 
    } 
} 

public class MyListItem 
{ 
    public string Title { get; set; } 
    public BitmapImage Image { get; set; } 
} 
관련 문제