RelativeSource binding in RadTreeView

5 posts, 1 answers
  1. Alex
    Alex avatar
    13 posts
    Member since:
    Aug 2010

    Posted 11 Aug 2010 Link to this post

    Hello,

        I've been using your version of TreeView in my SL application which I want to build using the MVVM pattern. I also need load-on-demand mechanism since I get the children of a TreeViewItem from a WCF service. I read your example about how to do this, but that example uses events in code-behind and as I've mentioned above I want to avoid that by using MVVM. So this is what I did:
    <telerik:RadTreeView x:Name="radTreeView"
                         Margin="0"
                         IsVirtualizing="True"
                         IsDragDropEnabled="True"
                         IsLoadOnDemandEnabled="True"
                         DataContext="{Binding SordsVM}"
                         ItemsSource="{Binding Children}"
                         ItemTemplate="{StaticResource SordViewModelTemplate}"
                         IsEditable="True"
                         AllowDrop="True"
                         IsExpandOnDblClickEnabled="False"
                         IsTextSearchEnabled="False">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="LoadOnDemand">
                <i:InvokeCommandAction Command="{Binding GetChildrenCommand}"
                                       CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </telerik:RadTreeView>
    (the "i:" namespace is xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity". I added this behavior from Blend 4.)

        My main issue is that when I am trying to bind the LoadOnDemand event to the GetChildrenCommand in my ViewModel and I set the CommandParameter using RelativeSource(I want to pass the DataContext of the TreeViewItem so I can get the ID of the expanded item and then call the WCF service which will get the children) what I get as parameter in my Execute method of the command is null.
    Is there an issue with RelativeSource binding in this case, or do I need to do something different since I'm using RadControls and not SL toolkit ones. 
        Also any other solution that could help me achieve the same thing would me much appreciated.

    Thanks,
    Alex.
  2. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 16 Aug 2010 Link to this post

    Hi Alex,


    The TemplatedParent can be used only with ControlTemplate in Silverlight. You can pass the DataContext or the SelectedItem instead, like this:

    <i:InvokeCommandAction Command="{Binding LoadChildEntitiesCommand}"
        CommandParameter="{Binding ElementName=SampleTreeView, Path=SelectedItem}" />

    or

    <i:InvokeCommandAction Command="{Binding LoadChildEntitiesCommand}"
        CommandParameter="{Binding ElementName=SampleTreeView, Path=DataContext}" />

    where SampleTreeView is the name of the RadTreeView.


    Sincerely yours,
    Hristo Milyakov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Alex
    Alex avatar
    13 posts
    Member since:
    Aug 2010

    Posted 17 Aug 2010 Link to this post

    Hello,

        Thanks for your answer but I have already tried binding the CommandParameter to the SelectedItem of the tree and indeed it works. But this does not help me in the case when somebody wants to expand one node without selecting it which is a pretty common scenario. If I bind to the DataContext of the tree then it won't help me either because I need the ViewModel of the expanded item not of the tree root.
        What can you recomand me about this ?

    Thanks for your time,
    Alex.
  5. Answer
    Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 20 Aug 2010 Link to this post

    Hi Alex,

    You can pass the event args of the particular LoadOnDemand event. However using this technique you need to implement your own version of InvokeCommandAction that provide those event args via some property. I've attached a working project demonstrating how to implement the suggested solution. The custom implementation of InvokeCommandAction is named InvokeDelegateCommandAction.

    Please take a look at the sample and check if it works for you.

    Best wishes,
    Hristo Milyakov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Alex
    Alex avatar
    13 posts
    Member since:
    Aug 2010

    Posted 23 Aug 2010 Link to this post

    Hello,

    Works like a charm! I should have thought of that as well to be honest :).
    Thanks a lot, keep up the good work.

    Alex.
Back to Top
DevCraft banner