I once wrote a WPF
app that loaded 52 'gif' images of playing cards into a uniform grid. I’ve fished out some of the code and posted it here in the hope that it may assist you.
From the xaml
.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid HorizontalAlignment="Center" >
<ListView ItemsSource="{Binding CardCollection }" >
<ListView.ItemTemplate>
<DataTemplate >
<Grid HorizontalAlignment="Center" >
<Image Source="{Binding CardGraphic}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel >
<ItemsPanelTemplate>
<UniformGrid Columns="13" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
From the viewModel.
private IEnumerable<Card> LoadImagesFromFile()
{
string fullCardsPath = Path.GetFullPath(Constants.RelativeFilePath);
var filenames = new List<String>(Directory.EnumerateFiles(fullCardsPath, Constants.AllImagesOfType));
return filenames.Select(fileName => new BitmapImage(new Uri(fileName))).Select((cardGraphic, i) => new Card(i, cardGraphic)).ToList();
}
public ObservableCollection<Card> CardCollection
{
get
{
return cardCollection;
}
set
{
cardCollection = value;
NotifyPropertyChanged();
}
}
The Card class has
public int Index { get; private set; }
public BitmapImage CardGraphic { get; private set; }
public Card(int index, BitmapImage cardGraphic)
{
Index = index;
CardGraphic = cardGraphic;
}
In my case, I defined the constants as
public static class Constants
{
public const string RelativeFilePath = @"..\..\Images";
public const string ImageType = ".gif";
public const string AllImagesOfType = "*.gif";
}