How to select New or Edited node in RadTreeView

2 posts, 0 answers
  1. Moon
    Moon avatar
    11 posts
    Member since:
    Jan 2012

    Posted 12 Mar 2012 Link to this post


    I am using RadTreeView in my WPF application and binding it to an object. Which is working as expected.

    Problem: Whenever I add new item in RadTreeView or edit any existing item, I reload my RadTreeView binding object. But I always end up with my either my root node as selected or child of root node. I want my new item (which I have added recently) or edited item to be selected. In my case I have a root node and it can have any number of child node, and those child nodes can have any number of child node. So I have 3 level hierarchy. But after I refresh my binding object (which is TreeViewDataSource in my case) I always end up with my second level and third level never expands.

    My XAML code for RadTreeView is:

    <Grid >
                                    <!--Data template for the Sensor object-->
                                    <HierarchicalDataTemplate x:Key="SensorTemplate">
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="/MORASS;component/Images/Sensor.png" />
                                            <TextBlock Text="{Binding SensorName}" Margin="2" />
                                    <!--Data template for the Logger object-->
                                    <!--<Image Source="/MORASS;component/Images/Logger.png">-->
                                    <HierarchicalDataTemplate x:Key="LoggerTemplate"
                                                              ItemTemplate="{StaticResource SensorTemplate}"
                                                              ItemsSource="{Binding Sensor}">
                                        <StackPanel Orientation="Horizontal">
                                                    <Style TargetType="{x:Type Image}">
                                                            <DataTrigger Binding="{Binding IsManualType}" Value="false">
                                                                <Setter Property="Source" Value="/MORASS;component/Images/Logger.png"/>
                                                            <DataTrigger Binding="{Binding IsManualType}" Value="true">
                                                                <Setter Property="Source" Value="/MORASS;component/Images/Logger-Manual.png"/>
                                            <TextBlock Text="{Binding LoggerDescription}" Margin="2" />
                                    <!--Data template for the Location object-->
                                            ItemsSource="{Binding Logger}"
                                            ItemTemplate="{StaticResource LoggerTemplate}">
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="/MORASS;component/Images/Location.png" />
                                            <TextBlock Text="{Binding LocationName}" Margin="2" />
                                <telerik:RadTreeView Name="radTreeViewMain" SelectedItem="{Binding SelectedItemNode}" IsLineEnabled="True" SelectedValuePath="{Binding Type}" SelectionChanged="radTreeViewMain_SelectionChanged"  >
                                    <telerik:RadTreeViewItem Header="Locations" IsExpanded="True" ContextMenu="{Binding Source={StaticResource TreeContextMenu}}" ItemsSource="{Binding TreeViewDataSource}" ItemTemplate="{StaticResource LocationTemplate}">

    Any help will be highly appreciated.

  2. Petar Mladenov
    Petar Mladenov avatar
    2882 posts

    Posted 15 Mar 2012 Link to this post

    Hello Moon,

     I am not sure I understood your scenario completely but several questions come up to my mind.
    Do you need to re-bind the RadTreeview when a new item is added or edited? If the TreeView is bound to ObservableCollection<> which implements the ICollectionChanged interface, whenever an item is added to this collection, the RadTreeView gets notified and the Layout of the tree is updated.
    You mention that you need to preserve the state of items when reloading/rebinding (with "state" I mean the values of the following properties - IsSelected,IsInEditMode, CheckState, IsExpanded). The best way to achieve this is to use Style Bindings(WPF 4, SL5) or  ContainerBindings (for SL4) - property from the ViewModel is bound to a corresponding property from the container - the RadTreeViewItem. You can check out this article for further reference.

    All the best,
    Petar Mladenov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft R3 2016 release webinar banner
Back to Top