RadDiagram Layout() & AutoFit() - when is the proper moment to use it?

6 posts, 2 answers
  1. Michał
    Michał avatar
    8 posts
    Member since:
    Jun 2013

    Posted 07 Oct 2013 Link to this post

    I'm adding shapes and connections manually in code behind to my RadDiagram. I'd like RadDiagram to layout shapes and fit it after adding last item, so i call RadDiagram.Layout(); and RadDiagram.AutoFit(); but that does not work. Why?

    private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                var shapes = new List<RadDiagramShape>();
     
                for (int i = 0; i <= 15; i++)
                {
                    var shape = new RadDiagramShape();
                    shapes.Add(shape);
     
                    if (i == 0)
                    {
                        _mainShape = shapes[0];
                    }
     
                    if (i > 0)
                    {
                        var conn = new RadDiagramConnection {Source = _mainShape, Target = shape, SourceCapType = CapType.Arrow1};
                        RadDiagram.Items.Add(conn);                   
                    }
                }
     
                RadDiagram.Items.AddRange(shapes);
     
                RadDiagram.Layout();
                RadDiagram.AutoFit();
            }
  2. Answer
    Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 10 Oct 2013 Link to this post

    Hi Michał,

    Thank you for contacting us on that matter. Let me get straight to your query.

    As I can see in your snippet that you try to Layout() and AutoFit() the RadDiagramShapes right after you add them to the Items collection of the RadDiagram component. Please note that the shapes are actually UIElements that must be rendered on the screen before using these methods. If the shapes are not yet visualized, the methods are not going to work as expected. This behavior is expected because the methods work with UIElements.

    In order to overcome this behavior you can invoke Layout() and AutoFit() in two dispatchers. You can use code similar to this:

    ...
    Dispatcher.BeginInvoke(() =>
                    {
                        RadDiagram.Layout();
                    });
    Dispatcher.BeginInvoke(() =>
                    {
                         RadDiagram.AutoFit();
                    });
    ...
    By doing so you will make sure that the shapes are visible in order to Layout() them. Furthermore, the second dispatcher will make sure that the shapes are arranged in order to fit them into the viewport of the RadDiagram.

    Please note that in Q2 2013 SP release we introduced LayoutAsinc() method which can be used instead the first dispatcher. This method waits for the shapes to be rendered on the screen before invoking the Layout() method.

    Please give this approach a try and let me know if you need any further assistance.

    Regards,
    Pavel R. Pavlov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Michał
    Michał avatar
    8 posts
    Member since:
    Jun 2013

    Posted 10 Oct 2013 Link to this post

    Hi Pavel,

    Thank you for a quick reply. The code modification you suggested has no results in my application. Is there a way to attach a simple solution to show you this behavior?
  5. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 15 Oct 2013 Link to this post

    Hi Michał,

    In order to send us a sample project you can log in your Telerik account and open a new support ticket. Once you do so, you will be allowed to attach a zip file. Please contact your license holder for log in details and open a new support ticket through your account.

    Thank you for understanding.

    Regards,
    Pavel R. Pavlov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  6. Michał
    Michał avatar
    8 posts
    Member since:
    Jun 2013

    Posted 18 Oct 2013 Link to this post

    As you stated in Q2 2013 SP release you introduced LayoutAsync() method which can be used instead the first dispatcher and this works like a charm but still I can't make diagram AutoFit. Can you help me with that?

    RadDiagram.LayoutAysnc();
    Dispatcher.BeginInvoke(() =>
                    {
                         RadDiagram.AutoFit();
                    });
  7. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 18 Oct 2013 Link to this post

    Hi Michal,

    Since this is a heavy operation, you can try using a DispatcherPriority - Loaded or ApplicationIdle as a second parameter of the BeginInvoke function.

    Regards,
    Petar Mladenov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top
UI for WPF is Visual Studio 2017 Ready