HierarchicalDataTemplate, ItemsSource and CollectionViewSource

Hello!

Is there a way to specify, in XAML, that a HierarchicalDataTemplate may have it's ItemsSource object to be wrapped by a certain CollectionViewSource (The instance of CollectionViewSource could for instance provide sorting of the ItemsSource collection)

If yes, may you provide a hint, please !

Best regards,

Henrik Dahl



Answer this question

HierarchicalDataTemplate, ItemsSource and CollectionViewSource

  • James Hendersen

    Hi Bea,

    In your blog I have found a lot of useful information regarding HierarchicalDataTemplates and TreeViews. In the last days I had some troubles with an advanced scenario in wich I am using a HierarchicalDataTemplate but I do not want the Itemspanel to be displayed the standard way. So I decided to post my question here but also to ask you for help because you are an expert in dealing with TreeViews, Templates and DataBinding.

    I posted my problem here:
    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2696307&SiteID=1&mode=1

    When you have some time I would be glad if you check my post and tell me if you know an answer.

    I also want to thank you for all these samples on your blog - they were a really great help for me :-)

    Greetings,
    - Simon

  • athomson

    Hi Henrik,

    The data binding team has been discussing that scenario pretty extensively lately. Unfortunately, the decision for V1 is not to support that, but we will for sure consider it again for V2. I think this will be a pretty common scenario, and we should allow it to be done all in XAML.

    There are some decisions on the current design that make this not so straight-forward to do. Most people attempt to do this by binding the HierarchicalDataTemplate's ItemsSource to some CollectionViewSource (either in the Resources or inline in the binding), and binding the CVS's Source to an IEnumerable property of the inherited DataContext. The problem here is that the CVS does not inherit the DataContext set in the HierarchicalDataTemplate, since it's not part of its visual tree.

    We will brainstorm this again for V2 and hopefully come up with a good solution. Receiving user's feedback like yours changes the priority of our work, so thanks for bringing this up.

    Bea



  • DayBreak

    Hi Henrik,

    Ok, I remember now the sample you were talking about. Unfortunately our parser does not support adding nested types to XAML, so currently it is not possible to do what you are asking. The only workaround I can think of is to do that in code.

    Thanks,

    Bea



  • objKiran

    Hi Bea,

    Thank you for your reply. Perhaps the external postponement of Windows Vista also makes up for an internal window for enhancements.

    Does "inline in the binding" mean that you can create an instance of CollectionViewSource inline in a binding If "yes", can't you show an example of how to do that, please

    Best regards,

    Henrik Dahl


  • John Hutchison

    OK. Currently I'm doing an ObservableCollection<MyEnumType> to which I add each of the entries in my enum. Is there a way to bind directly to the enum type itself so it's not necessary to update the contents of the ObservableCollection<MyEnumType> as the definition of MyEnumType evolves

    Best regards,

    Henrik Dahl


  • grantdi

    Hi again Bea!

    Thank you very much, also for your very quick response!

    Please allow me one question for your sample where you show a combobox having enum entries denoting T-shirt sizes. Is it also possible to have the enum defined inside the class instead of outside right inside the namespace If yes, may you show how the XAML should be updated to indicate passing through the class to the enum

    Best regards,

    Henrik Dahl


  • Mark R.

    Hi Henrik,

    Unfortunately there is a bug in FebCTP that prevents this from being possible to do in XAML. The correct markup to do this (which would work if we didn't have this bug) would be the following:

    <Window.Resources>
    <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="odp">
    <ObjectDataProvider.MethodParameters>
    <x:Type TypeName="local:MyEnumType"/>
    </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    </Window.Resources>

    <StackPanel>
    <ComboBox ItemsSource="{Binding Source={StaticResource odp}}" IsSynchronizedWithCurrentItem="true"/>
    </StackPanel>

    This is the XAML equivalent to calling

    Enum.GetValues(typeof(MyEnumType))

    through code, which returns an array with each of the enumeration values. I don't like being the one giving bad news to users, and it seems like I've been doing that a lot with you recently :) Please look for a fix to this issue in a future public release.

    Bea



  • FrameTech

    Hi Erno,

    I actually explain this scenario in my last blog post, and have the sample available for download in Beta 2 bits. So yes, as you already figured out, this now works. :)

    Bea



  • Poshi

    Hi Bea,

    It's your DataTriggerEnum sample. I'm sorry, it was not about comboboxes, but my corrected question goes here: The enum I have on mind is the TShirtSizes enum defined in Window1.xaml.cs. My question basically is if you could try to show how to write the Window1.xaml code addressing the Value for the DataTrigger where you have moved the definition of the TShirtSizes enum inside the DataSource class, please

    Best regards,

    Henrik Dahl


  • tttsss

    Found it! Yes it is working; I just forgot to add the green part:

    xmlns:sys="clr-namespace:System;assembly=mscorlib"


  • sourind

    Bea,

    Has this issue been solved in Beta 2 I keep on running into an XamlParseException that states:

    {"Error at element 'String' in markup file '09_CustomDataSource;component/window1.xaml' : 'sys:Enum' string cannot be converted to object of type 'System.Type'."}

    Thanks!


  • shaggymog

    Hi Henrik,

    Yes, you got it, that is exactly what I meant. Here is an example of that:

    <ListBox DisplayMemberPath="Name">
    <ListBox.ItemsSource>
    <Binding>
    <Binding.Source>
    <CollectionViewSource Source="{StaticResource source}">
    <CollectionViewSource.SortDescriptions>
    <scm:SortDescription PropertyName="Name" Direction="Descending"/>
    </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
    </Binding.Source>
    </Binding>
    </ListBox.ItemsSource>
    </ListBox>

    I uploaded a working sample with this markup for you here: http://www.beacosta.com/Forum/InlineCVS.zip.

    Thanks,

    Bea



  • Robert te Kaat - IS

    Hi Henrik,

    I don't remember coding that sample, could you be confusing me with someone else But if you point me to it I can try to answer your question.

    Bea



  • HierarchicalDataTemplate, ItemsSource and CollectionViewSource