WPF user control - ItemsControl not applied

I am creating a WPF user control that comes from TabControl. In the ControlTemplate, I use ItemControl to display the list associated with the template (an observable collection of type FileMenuItem). While the program is running, I get the following error in the output window:

ItemTemplate and ItemTemplateSelector are ignored for items that are already Container Type ItemsControl; Type = 'FileMenuItem'

The FileMenuItem type is inferred from MenuItem. If I changed the base class to DependencyObject, the code really runs and the template is applied (so there is an option). I googled a bug and couldn't find anything about this, does anyone encounter this while developing custom controls? Despite the fact that I have a workaround, I would like to understand what is happening, and I think that using MenuItem as a base class is a cleaner implementation.

I can post more code if this helps. Thanks!

+7
c # wpf
source share
1 answer

The purpose of a DataTemplate (e.g., ItemTemplate) is to provide a visualization for a data object. In particular, it defines a set of elements to add to the visual tree instead of the data provided by ContentPresenter or ItemsPresenter. In your case, your source list is a set of objects that can already be added directly to the visual tree for display in the user interface.

You can see this in the following simplified example, where only โ€œThreeโ€ is displayed in โ€œRedโ€ because the first two elements are defined in a form that can be displayed directly by the ComboBox.

<ComboBox> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" Foreground="Red"/> </DataTemplate> </ComboBox.ItemTemplate> <ComboBoxItem>One</ComboBoxItem> <ComboBoxItem>Two</ComboBoxItem> <sys:String>Three</sys:String> </ComboBox> 
+6
source share

All Articles