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

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

5 Answers 365 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Michał
Top achievements
Rank 1
Michał asked on 07 Oct 2013, 12:35 PM
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();
        }

5 Answers, 1 is accepted

Sort by
0
Accepted
Pavel R. Pavlov
Telerik team
answered on 10 Oct 2013, 09:53 AM
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 >>
0
Michał
Top achievements
Rank 1
answered on 10 Oct 2013, 10:49 AM
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?
0
Pavel R. Pavlov
Telerik team
answered on 15 Oct 2013, 10:48 AM
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 >>
0
Michał
Top achievements
Rank 1
answered on 18 Oct 2013, 08:10 AM
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();
                });
0
Accepted
Petar Mladenov
Telerik team
answered on 18 Oct 2013, 02:37 PM
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 >>
Tags
Diagram
Asked by
Michał
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Michał
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or