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

How to select New or Edited node in RadTreeView

1 Answer 163 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Moon
Top achievements
Rank 1
Moon asked on 12 Mar 2012, 01:13 PM
Hi,

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 >
                            <Grid.Resources>
                                <!--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" />
                                    </StackPanel>
                                </HierarchicalDataTemplate>
                                <!--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">
                                        <Image>
                                            <Image.Style>
                                                <Style TargetType="{x:Type Image}">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsManualType}" Value="false">
                                                            <Setter Property="Source" Value="/MORASS;component/Images/Logger.png"/>
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding IsManualType}" Value="true">
                                                            <Setter Property="Source" Value="/MORASS;component/Images/Logger-Manual.png"/>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Image.Style>
                                        </Image>
                                        <TextBlock Text="{Binding LoggerDescription}" Margin="2" />
                                    </StackPanel>
                                </HierarchicalDataTemplate>
                                <!--Data template for the Location object-->
                                <HierarchicalDataTemplate  
                                        x:Key="LocationTemplate"  
                                        ItemsSource="{Binding Logger}"
                                        ItemTemplate="{StaticResource LoggerTemplate}">
                                    <StackPanel Orientation="Horizontal">
                                        <Image Source="/MORASS;component/Images/Location.png" />
                                        <TextBlock Text="{Binding LocationName}" Margin="2" />
                                    </StackPanel>
                                </HierarchicalDataTemplate>
                            </Grid.Resources>
                            <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}">
                                    
                                </telerik:RadTreeViewItem>
                            </telerik:RadTreeView>
</Grid>


Any help will be highly appreciated.

Regards,
Moon

1 Answer, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 15 Mar 2012, 07:45 AM
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 >>
Tags
TreeView
Asked by
Moon
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Share this question
or