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

Problem adding a child to self-referencing tree view.

1 Answer 59 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Rodd
Top achievements
Rank 1
Rodd asked on 08 Aug 2011, 04:09 PM

I have the following xaml in my View.

<tel:RadTreeView x:Name="UnitTree"
                 ItemsSource="{Binding Units, Converter={StaticResource UnitHierarchy}}"
                 SelectedItem="{Binding SelectedUnit, Mode=TwoWay}"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 IsDragDropEnabled="True"
                 BorderBrush="Black" BorderThickness="1"
                 Grid.Row="1" Grid.ColumnSpan="2"
                 IsExpandOnSingleClickEnabled="True">
    <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"  />
                </StackPanel>
                <TextBlock x:Name="type" Text="{Binding UnitType}" FontStyle="Italic" FontSize="10" Foreground="Gray" FontWeight="Bold" />
            </StackPanel>
        </tel:HierarchicalDataTemplate>
    </tel:RadTreeView.ItemTemplate>
</tel:RadTreeView>
<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}" />
</StackPanel>

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; }
        set
        {
            if (_selectedUnit != value)
            {
                if (_selectedUnit != null && (_selectedUnit.HasChanges || _selectedUnit.EntityState == System.ServiceModel.DomainServices.Client.EntityState.New))
                {
                    MessageBox.Show("Need to save first.");
                    SelectedUnit = _selectedUnit;
                }
                else
                {
                    var oldValue = _selectedUnit;
                    _selectedUnit = value;
                    RaisePropertyChanged("SelectedUnit", oldValue, value, true);
                    AddChildCommand.RaiseCanExecuteChanged();
                    RemoveCommand.RaiseCanExecuteChanged();
                }
            }
        }
    }
 
    private void AddChild()
    {
        var child = new Unit() { Id = newUnitId--, Name = "New Child Unit", ParentId = SelectedUnit.Id };
        Units.Add(child);
        var unit = SelectedUnit;
        Messenger.Default.Send<string>("ExpandSelectedItem");
        RaisePropertyChanged("Units");
        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()
    {
        InitializeComponent();
        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!

1 Answer, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 11 Aug 2011, 02:55 PM
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.

Greetings,
Tina Stancheva
the Telerik team

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

Tags
TreeView
Asked by
Rodd
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or