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

How to bind SelectedItem to a ViewModel property when TreeView is populated from XML using XMLDataProvider

1 Answer 168 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Divya asked on 13 Oct 2014, 11:42 AM

I have a RadTreeView where data is populated from an xml using XmlDataProvider.  I would like to bind the SelectedItem of TreeView to a ViewModel property of type string. When I do the binding using SelectedItem={Binding mydata}", the value mydata is set to System.Xml.XmlElement type.

I was able to find the logic to get the exact value. For testing purpose, I created a new TextBox control to retreive the exact selected data.

 <TextBox  DataContext="{Binding ElementName=ReportersTreeView, Path=SelectedItem}" Grid.Row="6"   Text="{Binding XPath=@Name}"></TextBox>

However in real scenario, I don't need this TextBox. 

I want to know how can I integrate this code to my tree view control such that XPath=@Name is correctly retrieved from SelectedItem  and set to my viewmodel property?

Below is my TreeView control and my Template.

 <HierarchicalDataTemplate x:Key="NodeTemplate">
            <TextBlock x:Name="tb"/>
                <Binding XPath="child::node()" />
                <!--<DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                    <Setter TargetName="tb" Property="Text" Value="{Binding XPath = @name}"></Setter>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                    <Setter TargetName="tb" Property="Text" Value="{Binding Path=Attributes[Name].Value}"></Setter>
        <XmlDataProvider x:Key="ReportersDataProvider" Source="ProbesTree.xml"

 <telerik:RadTreeView Grid.Row="2"  IsDragDropEnabled="True" Name="ReportersTreeView"  SelectionMode="Single" IsSingleExpandPath="False" IsLineEnabled="True"   AutomationProperties.AutomationId ="ReportersTreeView" VerticalAlignment="Stretch"
                ItemTemplate= "{StaticResource NodeTemplate}" ItemsSource="{Binding Source={StaticResource ReportersDataProvider}}"  VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"/>

Please help.!!!!


1 Answer, 1 is accepted

Sort by
Pavel R. Pavlov
Telerik team
answered on 15 Oct 2014, 09:24 AM
Hi Divya,

When working with the selection of the RadTreeView control you should keep in mind that there are some known limitations regarding the RadTreeView.SelectedItem and RadTreeView.SelectedItems properties. This is why we suggest using the IsSelected property of the RadTreeViewItem. You can take advantage of it by exposing a boolean property in your ViewModel and binding it using a custom Style targeting the RadTreeViewItem control.

I used our documentation article to create a sample project which takes advantage of the XML DataProvider. I exposed the boolean IsSelected property in the XmlNodeItem class and its setter I update a static property holding the business object behind the selected RadTreeViewItem. This approach is demonstrated in the attached project. Please take a look at it and let me know if you need any further assistance.

Pavel R. Pavlov

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Asked by
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Share this question