Problem adding a child to self-referencing tree view.

2 posts, 0 answers
  1. Rodd
    Rodd avatar
    7 posts
    Member since:
    Jul 2010

    Posted 08 Aug 2011 Link to this post

    I have the following xaml in my View.

    <tel:RadTreeView x:Name="UnitTree"
                     ItemsSource="{Binding Units, Converter={StaticResource UnitHierarchy}}"
                     SelectedItem="{Binding SelectedUnit, Mode=TwoWay}"
                     BorderBrush="Black" BorderThickness="1"
                     Grid.Row="1" Grid.ColumnSpan="2"
        <tel:RadTreeView.ItemTemplate >
            <tel:HierarchicalDataTemplate ItemsSource="{Binding Converter={StaticResource UnitHierarchy}}" >
                <StackPanel Orientation="Vertical" Width="200" >
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock x:Name="name" TextWrapping="Wrap" Text="{Binding Name}" FontWeight="Bold" />
                        <TextBlock Text="*" Margin="10,0,0,0"  />
                    <TextBlock x:Name="type" Text="{Binding UnitType}" FontStyle="Italic" FontSize="10" Foreground="Gray" FontWeight="Bold" />
    <StackPanel Orientation="Horizontal" Margin="0,10,0,5" Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Right">
        <Button Content="Add Child" Command="{Binding AddChildCommand}" Style="{StaticResource AppButton}" />
        <Button Content="Remove" Command="{Binding RemoveCommand}" Style="{StaticResource AppButton}"/>
        <Button Content="Save" Command="{Binding SaveCommand}" Style="{StaticResource AppButton}" />

    I'm binding to self-referencing data following the technique described here: Binding a treeview to self-referencing data.

    In my view model, I have the following:
    public class UnitNavigatorViewModel : ViewModelBase
        private int newUnitId = -1;
        public RelayCommand AddChildCommand { get; private set; }
        public UnitCollection Units { get; set; }
        private Unit _selectedUnit;
        public Unit SelectedUnit
            get { return _selectedUnit; }
                if (_selectedUnit != value)
                    if (_selectedUnit != null && (_selectedUnit.HasChanges || _selectedUnit.EntityState == System.ServiceModel.DomainServices.Client.EntityState.New))
                        MessageBox.Show("Need to save first.");
                        SelectedUnit = _selectedUnit;
                        var oldValue = _selectedUnit;
                        _selectedUnit = value;
                        RaisePropertyChanged("SelectedUnit", oldValue, value, true);
        private void AddChild()
            var child = new Unit() { Id = newUnitId--, Name = "New Child Unit", ParentId = SelectedUnit.Id };
            var unit = SelectedUnit;
            SelectedUnit = unit;
            SelectedUnit = child;
        //AddChildCommand is defined as
            AddChildCommand = new RelayCommand(AddChild, () => SelectedUnit != null);

    Lastly, my View code behind looks like this:
    public partial class EntityNavigator : UserControl
        public EntityNavigator()
            Messenger.Default.Register<string>(this, HandleMessage);
        private void HandleMessage(string message)
            if (message == "ExpandSelectedItem")
                this.UnitTree.SelectedContainer.IsExpanded = true;

    What I'm trying to accomplish is this.  I want to select a node and click add child.  I then want the node to expand, display the child and show the child as selected.

    The problem I'm having is that the code in the view code behind is throwing an error because the minute I RaisePropertyChanged("Units") to cause the list to be refreshed, SelectedContainer gets set to null.  So, trying to set IsExpanded fails.

    Thanks for your help!
  2. Tina Stancheva
    Tina Stancheva avatar
    3299 posts

    Posted 11 Aug 2011 Link to this post

    Hello Rodd,

    From the code snippets you sent I couldn't get the full picture of your view models logic. This is why I attached a sample project illustrating how you can implement your scenario.

    Basically, since you want to select a newly added RadTreeViewItem and at the same time expand its parent, I'd suggest you to bind the RadTreeViewItems' IsSelected and IsExpanded properties. The attached solution utilizes such an approach. Please have a look at it and let us know if it helps.

    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Back to Top