{"record":{"id":"bbde335440f6938f","repo":"dotnet/maui","slug":"the-map-itemtemplate-property-only-supports-datate","errorCode":null,"errorMessage":"The Map.ItemTemplate property only supports DataTemplate. Set the Map.ItemTemplateSelector property instead to use a DataTemplateSelector","messagePattern":"The Map\\.ItemTemplate property only supports DataTemplate\\. Set the Map\\.ItemTemplateSelector property instead to use a DataTemplateSelector","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Controls/Maps/src/Map.cs","lineNumber":287,"sourceCode":"\t\t\tif (oldItemsSource is INotifyCollectionChanged ncc)\n\t\t\t{\n\t\t\t\tncc.CollectionChanged -= OnItemsSourceCollectionChanged;\n\t\t\t}\n\n\t\t\tif (newItemsSource is INotifyCollectionChanged ncc1)\n\t\t\t{\n\t\t\t\tncc1.CollectionChanged += OnItemsSourceCollectionChanged;\n\t\t\t}\n\n\t\t\t_pins.Clear();\n\t\t\tCreatePinItems();\n\t\t}\n\n\t\tvoid OnItemTemplatePropertyChanged(DataTemplate oldItemTemplate, DataTemplate newItemTemplate)\n\t\t{\n\t\t\tif (newItemTemplate is DataTemplateSelector)\n\t\t\t{\n\t\t\t\tthrow new NotSupportedException(\n\t\t\t\t\t$\"The {nameof(Map)}.{ItemTemplateProperty.PropertyName} property only supports {nameof(DataTemplate)}.\" +\n\t\t\t\t\t$\" Set the {nameof(Map)}.{ItemTemplateSelectorProperty.PropertyName} property instead to use a {nameof(DataTemplateSelector)}\");\n\t\t\t}\n\n\t\t\t_pins.Clear();\n\t\t\tCreatePinItems();\n\t\t}\n\n\t\tvoid OnItemTemplateSelectorPropertyChanged()\n\t\t{\n\t\t\t_pins.Clear();\n\t\t\tCreatePinItems();\n\t\t}\n\n\t\tvoid OnItemsSourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)\n\t\t{\n\t\t\te.Apply(\n\t\t\t\tinsert: (item, _, __) => CreatePin(item),","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/Maps/src/Map.cs#L269-L305","documentation":"The Map control separates its templating into two properties: ItemTemplate (for a single DataTemplate) and ItemTemplateSelector (for a DataTemplateSelector that chooses templates per-item). Assigning a DataTemplateSelector instance to ItemTemplate throws NotSupportedException because the internal CreatePinItems logic only handles uniform DataTemplate rendering, and silently ignoring the selector would produce wrong results.","triggerScenarios":"Setting `map.ItemTemplate = new MyDataTemplateSelector()` where MyDataTemplateSelector derives from DataTemplateSelector. This can happen when a developer is unaware of the two-property split and assigns a selector to the simpler property.","commonSituations":"Copy-pasting a DataTemplateSelector from a ListView/CollectionView sample into Map.ItemTemplate instead of Map.ItemTemplateSelector. Refactoring code that previously used a single template to use a selector without moving the assignment. Misreading the API surface.","solutions":["Use Map.ItemTemplateSelector instead of Map.ItemTemplate when you need per-item template selection.","If you only need one template, assign it to Map.ItemTemplate directly.","Verify the type of the object you are assigning: if it is a DataTemplateSelector subclass, use ItemTemplateSelector."],"exampleFix":"// before\nmap.ItemTemplate = new PinTemplateSelector(); // DataTemplateSelector subclass\n\n// after\nmap.ItemTemplateSelector = new PinTemplateSelector();","handlingStrategy":"type-guard","validationCode":"// Check before assigning\nif (template is DataTemplateSelector)\n    map.ItemTemplateSelector = (DataTemplateSelector)template;\nelse\n    map.ItemTemplate = template;","typeGuard":"static bool IsDataTemplateSelector(object template) => template is DataTemplateSelector;","tryCatchPattern":null,"preventionTips":["Read the Map API: ItemTemplate is for a single DataTemplate, ItemTemplateSelector is for selectors.","When refactoring from single template to selector, move the assignment to ItemTemplateSelector.","Add a unit test asserting that assigning a DataTemplateSelector to ItemTemplate throws NotSupportedException."],"tags":["maui","maps","datatemplate","datatemplateselector","not-supported","itemtemplate"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}