XAML
<Window x:Class="ListBoxImage.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ListBoxImage"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="52,65,109,97">
<ListBox ItemsSource="{Binding VM_Model}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="6"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Image Margin="3" Source="{Binding Path}" Width="100" Height="100" Stretch="Fill"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
MainWindow
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
ViewModel
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ViewModel()
{
VM_Model = new ObservableCollection<Model>();
for (int i = 0; i < 5; i++)
{
VM_Model.Add(new Model() { Path = Path.Combine(Directory.GetCurrentDirectory(), "imges", i+".jpg") });
}
}
private ObservableCollection<Model> vm_model { get; set; }
public ObservableCollection<Model> VM_Model
{
get
{
return vm_model;
}
set
{
vm_model = value;
OnPropertyChanged("VM_Model");
}
}
}
public class Model
{
public string Path { get; set; }
}