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

2 posts, 0 answers
  1. Divya
    Divya avatar
    6 posts
    Member since:
    Apr 2014

    Posted 13 Oct 2014 Link to this post

    Hi,

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

     <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.!!!!

    Thanks,
    Divya
             
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 15 Oct 2014 Link to this post

    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.

    Regards,
    Pavel R. Pavlov
    Telerik
     

    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.

     
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top