ItemsControl region: DataTemplate ignored

Oct 8, 2010 at 3:11 PM
Edited Oct 8, 2010 at 3:13 PM

After changing the following region from ListBox:

<ListBox cal:RegionManager.RegionName="{x:Static R}">
  <ListBox.ItemTemplate>
    <DataTemplate>
         <GroupBox Header="{Binding DataContext.Header}" >
              <ContentControl Content="{Binding}" />
         </GroupBox>
      </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

to ItemsControl:

<ItemsControl cal:RegionManager.RegionName="{x:Static R}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
         <GroupBox Header="{Binding DataContext.Header}" >
              <ContentControl Content="{Binding}" />
         </GroupBox>
      </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

I get the (runtime) error message: (MyView is a UserControl added to the region elsewhere.)
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='MyView'

and as it says, the ItemTemplate is completly ignored!

I think it works with the ListBox because it requires ListBoxItems, but ItemsControl accepts it as an UIElement.
Can I somehow get this to work without changing the code that adds MyView (or MyView itself) to the region?
Oct 8, 2010 at 7:35 PM

Hi,

I think that you can find interesting the following blog post:

On the other hand, as this could be more related to WPF/Silverlight and not to Prism scenarios, you could ask this question in the following forums, where you might get more accurate guidance:

Fernando Antivero
http://blogs.southworks.net/fantivero

Oct 11, 2010 at 8:16 AM

Hi

Thanks for your answer. Very interesting blog post (series). I think I found a workaround in the sequel ItemsControl: 'G' is for Generator:

  public class WorkaroundItemsControl : ItemsControl
  {
      protected override bool IsItemItsOwnContainerOverride(object item)
      {
          return false;
      } 
  }

FYI I posted here on the Prism/CAL/compositewpf forum since this problem (in my opinion) undermines the whole point of the
composite approach / its region system.
If changing a minor detail in one component's UI (e.g., changing a ListBox to a ItemsControl) breaks functionality, and the only apparent solution is a larger change in potentially many other components (e.g. wrapping UIElement based views in an additional workaround class) that is a serious problem.
In my opinion o serious that the Prism community should be aware of it and search and publicize a solution or
advocate changes in WPF if no solution is possible.

Cheers
Oct 12, 2010 at 3:05 PM

Hi,

Thanks for sharing this with the rest of the community, since it might help other users in a similar scenario as you mentioned. I am glad that you found a possible workaround for achieve this scenario.

Fernando Antivero
http://blogs.southworks.net/fantivero