none
Problem with Data Template Selection RRS feed

  • Question

  • I am attempting to use a HeaderTemplateSelector with a GroupStyle.  I wrote a custom class that inherits from DataTemplateSelector, and implemented the various properties and SelectTemplate method override.  The problem I am running into is that "Object item" at runtime is of type MS.Internal.Data.CollectionViewGroupInternal.  The examples I have seen on data template selectors show that the item parameter is of the element type of my collection.

    public
    override DataTemplate SelectTemplate(Object item, DependencyObject container)

    When I inspect the item in the debugger, I see that there's a property called SeedItem that is of the correct type.  The problem is I can't cast item to this CollectionViewGroupInternal type to get at the SeedItem property because the compiler gives me an error:

        'MS.Internal.Data.CollectionViewGroupInternal' is inaccessible due to its protection level

    But I'd rather not have to go there anyway.  I just want item to be of the type I am expecting.

    The only difference I can see with what I'm doing and the other examples on data template selectors is that I'm using a CollectionViewSource to group my listbox items.  I am assuming that WPF creates a view behind the scenes of type CollectionViewGroupInternal.  But how can I get to my object at runtime in the SelectTemplate method so I can determine which data template to use?

    Here are some snippets of my code.  Hopefully someone can help.  This has been a real tough one!!

    -Larry

    ====================================

    <ListBox x:Name="projectItems" SelectedIndex="0"

    Background="{x:Null}" BorderThickness="0"

    ItemsSource="{Binding Path=ProjectList}"

    ItemTemplate="{StaticResource projectListTemplate}"

    ScrollViewer.HorizontalScrollBarVisibility="Disabled"

    SelectionMode="Single">

    <ListBox.GroupStyle>

    <GroupStyle HeaderTemplateSelector="{StaticResource scheduleGroupHeaderSelector}">

    ====================================

    <local:ScheduleGroupHeaderSelector x:Key="scheduleGroupHeaderSelector"

    ReleaseProjectTemplate="{StaticResource scheduleGroupHeaderReleaseProjectTemplate}"

    MaintenanceProjectTemplate="{StaticResource scheduleGroupHeaderMaintenanceProjectTemplate}" />

    ====================================

    ICollectionView view = CollectionViewSource.GetDefaultView(projectItems.ItemsSource);

    view.GroupDescriptions.Add(new PropertyGroupDescription("ProjectTypeName"));

    view.SortDescriptions.Add(new SortDescription("ProjectTypeName", ListSortDirection.Descending));

    view.SortDescriptions.Add(new SortDescription("ProjectName", ListSortDirection.Ascending));




    Wednesday, July 30, 2008 7:43 PM

Answers

  • CollectionViewGroupInternal is derived from CollectionViewGroup, and CollectionViewGroup has a public properties called Items which you can use to examine the type of the items within it.

    Hope this helps
    Monday, August 4, 2008 7:21 AM

All replies

  • Reflection to the rescue!

    Type type = item.GetType();

    PropertyInfo pi = type.GetProperty("SeedItem", BindingFlags.NonPublic | BindingFlags.Instance);

    ProjectWorksheetInfo project = (ProjectWorksheetInfo)pi.GetValue(item, null);

    if (project.ProjectTypeId == ProjectTypeValues.ReleaseProjectId)

    return ReleaseProjectTemplate;

    else

    return MaintenanceProjectTemplate;


    So everything is working great, but I still don't like the fact that I'm accessing an internal class, and then using reflection to get to a nonpublic member.

    Can someone please advise if this is the best way to do this?  Perhaps there's a more elegant way in .NET 3.5 SP1?

    Thanks.
    -Larry
    Friday, August 1, 2008 12:01 PM
  • CollectionViewGroupInternal is derived from CollectionViewGroup, and CollectionViewGroup has a public properties called Items which you can use to examine the type of the items within it.

    Hope this helps
    Monday, August 4, 2008 7:21 AM
  • Thank you, that is a much better approach than using reflection!!
    Monday, August 4, 2008 11:51 AM