This is a migrated thread and some comments may be shown as answers.

RelativeSource binding in RadTreeView

4 Answers 141 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 2
Alex asked on 11 Aug 2010, 02:53 PM
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.

4 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 16 Aug 2010, 05:31 PM
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
0
Alex
Top achievements
Rank 2
answered on 17 Aug 2010, 01:57 PM
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.
0
Accepted
Hristo
Telerik team
answered on 20 Aug 2010, 02:34 PM
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
0
Alex
Top achievements
Rank 2
answered on 23 Aug 2010, 08:54 AM
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.
Tags
TreeView
Asked by
Alex
Top achievements
Rank 2
Answers by
Hristo
Telerik team
Alex
Top achievements
Rank 2
Share this question
or