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

AutoLayout issue after adding items to GraphSource

2 Answers 115 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Dominik
Top achievements
Rank 1
Dominik asked on 18 Aug 2020, 02:34 PM

Hi,

I tried adding a node and a link to the GraphSource. It worked but after this operation, the diagram layout seems to have been reset even though the AutoLayout property is set to True.

MainWindow.xaml.cs

using System;
using System.Windows;
using radDiagramTest.ViewModels;
using Telerik.Windows.Diagrams.Core;

namespace radDiagramTest
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      ClassificationGraphViewModel viewModel = RootGrid.Resources["ViewModel"] as ClassificationGraphViewModel;

      ClassificationDiagram.GraphSource = viewModel?.GraphSource;

      ClassificationDiagram.Loaded += ClassificationDiagram_Loaded;
      ClassificationDiagram.AutoFit( new Thickness( 10 ), false );
    }

    private void ClassificationDiagram_Loaded( Object _sender, RoutedEventArgs _e )
    {
      TreeLayoutSettings settings = new TreeLayoutSettings
      {
        TreeLayoutType = TreeLayoutType.TreeDown,
        HorizontalSeparation = 300d,
        VerticalSeparation = 75d
      };

      settings.Roots.Add( ClassificationDiagram.Shapes[0] );

      ClassificationDiagram.Layout( LayoutType.Tree, settings );
      ClassificationDiagram.AutoLayout = true;
      ClassificationDiagram.IsEditable = false;
    }
  }
}

NodeViewModel.cs (node custom view model where the Add button is)

using System;
using System.Windows;
using System.Windows.Input;
using radDiagramTest.ViewModels;
using Telerik.Windows.Controls.Diagrams.Extensions.ViewModels;

namespace radDiagramTest
{
  public class NodeViewModel : HierarchicalNodeViewModel
  {
    public NodeViewModel( ClassificationGraphViewModel _parent )
    {
      m_Parent = _parent;

      AddNodeCommand = new SimpleCommand( AddNodeCommandHandler );
    }

    public ICommand AddNodeCommand { get; set; }
    public String Name { get; set; }

    private void AddNodeCommandHandler()
    {
      NodeViewModel newNode = new NodeViewModel( m_Parent ) { Name = "New Node" };
      m_Parent.GraphSource.AddNode( newNode );

      LinkViewModel newLink = new LinkViewModel( this, newNode ) { Name = "New Edge" };
      m_Parent.GraphSource.AddLink( newLink );
    }

    private readonly ClassificationGraphViewModel m_Parent;
  }
}

2 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 21 Aug 2020, 01:21 PM

Hello Dominik,

The reported behavior appears because by default the RadDiagram auto layout feature uses the default layout settings of the diagram. So, when you add/remove items in the GraphSource, the default settings kick-in and the layout looks different than the expected. To resolve this, you can use the LayoutAsync() instead of Layout() method. This way your settings will be cached and used later by the auto layout feature.

 ClassificationDiagram.LayoutAsync( LayoutType.Tree, settings );

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Dominik
Top achievements
Rank 1
answered on 21 Aug 2020, 02:00 PM

Hi Martin,

 

Problem fixed :)!

Thanks for your help

Tags
Diagram
Asked by
Dominik
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Dominik
Top achievements
Rank 1
Share this question
or