您可以像这样绑定到集合中的项目:
<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; }
}