I have a WPF Window with a Canvas that is used to show a terrain map. I would like to place symbols on this map according to an IEnumerable<MapSymbolDefinition> where MapSymbolDefinition is defined as follows:
public class MapSymbolDefinition
{
string SymbolFileName { get; set; }
int XLocation { get; set; }
int YLocation { get; set; }
}
All symbols are stored in the Symbols folder of my project, so the path to any symbol would be [ProjectFolder]/Symbols/SymbolFileName.bmp.
Any thoughts on how I can set this up so I don’t have to populate a bunch of Image objects manually in the code-behind?
Thanks.
Use an
ItemsControlwith customizedItemsPanel,ItemContainerStyleandItemTemplate:Update after comments: You can define an
ItemTemplateto provide the visual tree for each item in the collection. In this case I used a converter to convert theSymbolFileNameproperty to an image, but you could equally define a separate property on yourMapSymbolDefinitionclass that yields the full path. The latter approach is more in line with MVVM best practices.