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

Group, Align and BringToFront etc. Functions

3 Answers 58 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Barış
Top achievements
Rank 1
Barış asked on 27 Aug 2015, 07:20 AM

Hello, 

I need to implement Group, Align, BringToFront and such functions for my project. Unfortunately, my diagram and ribbon views are in seperate Regions (We use MVVM with prism). So I need to implement a code block to provide these functionality. 

I found a code piece in these forum to replace the BringToFront function but It didn't run good. 

public void Arrange(string param)
{
    if (!this.Diagram.SelectedItems.IsNullOrEmpty())
    {
        IEnumerable<IDiagramItem> items = this.Diagram.SelectedItems.Select(i => this.Diagram.ContainerGenerator.ContainerFromItem(i));
 
        if (param == "Top")
        {
            this.Diagram.BringToFront(items, true);
        }
        else if (param == "Forward")
        {
            this.Diagram.BringForward(items, true);
        }
        else if (param == "Back")
        {
            this.Diagram.SendToBack(items, true);
        }
        else if (param == "Backward")
        {
            this.Diagram.SendBackward(items, true);
        }
    }
}

  I also implement some other codes to provide Group and Align function accordingly. Can you suggest a new way. 

public void Group()

{

    if (!this.Diagram.SelectedItems.IsNullOrEmpty())
    {
        var items = new List<IGroupable>();
        foreach (var item in this.Diagram.SelectedItems)
        {
            items.Add(item as IGroupable);
        }
 
        this.Diagram.Group(string.Empty, true, items.ToArray());
    }
}

 

public void Align(Alignment alignment)
{
    if (!this.Diagram.SelectedItems.IsNullOrEmpty())
    {
        var items = new List<IShape>();
        foreach (var item in this.Diagram.SelectedItems.Where(x => x is IShape))
        {
            items.Add(item as IShape);
        }
 
        this.Diagram.Align(alignment, items);
    }
}

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 31 Aug 2015, 12:48 PM
Hi Barış,

I am not sure that I understand what is your concern. The code snippets looks correct for the functions that you want to achieve in your application. Can you elaborate a little more on your scenario by answering the following questions:
  • Can you tell me what you mean by saying that the code didn't run good?
  • Can you send me pictures of the actual and the expected result?
  • Can you send me runnable code snippets or a project that demonstrates an isolated version of your application so I can check it on my side?
This will help me in better understanding your scenario and assist you further. In addition, you can take a look at the RadDiagram demos too see how those features are implemented in real life application examples.

Thank you for any help you can provide.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Barış
Top achievements
Rank 1
answered on 01 Sep 2015, 10:46 AM

Hi Martin,

Let me explain your questions. 

  • I mean that application throws an exception ​by saying "the code did'nt run good." You can find detailed explanation below.
  • I think there is no need to add a picture. I want the code to run as how the Built-in commands (Group, align, bringtofront) runs. Actual state is an exception, which you can find below. 
  • Actually you can show how to trigger these functionalities using code in mvvm scenario.

And In the raddiagram demos, this scenario is not mentioned. 

 For Group Function,

items.Add(item as IGroupable);

These line creates null values in collection, which results with "Null Reference Exception" in the line below.

this.Diagram.Group(string.Empty, true, items.ToArray());

 For Align Function

 this.Diagram.SelectedItems.Where(x => x is IShape)

 This line returns nothing because the SelectedItems contains NodeViewModelBase instances( Actually my, own derived class). So there is no exception but, nothing changes on UI side.  

The Arrange function  

This function works as charm now. I think the test process was wrong. 

Thank you in advance,

Regards

0
Accepted
Martin Ivanov
Telerik team
answered on 03 Sep 2015, 09:35 AM
Hello Barış,

Thank you for the additional information. Based on it I prepared a small example that demonstrates grouping and aligning in an MVVM scenario. About the exception, it is thrown because the Group() method of the diagram expects the elements in the "items" argument to be IGroupable elements and if you there is a null value an exception is thrown. To resolve this you can use the diagram's ContainerGenerator and generate the RadDiagramShape elements that presents the view model. Then pass this shape in the "items" collection. Here is an example:

var container = this.Diagram.ContainerGenerator.ContainerFromItem(item);
items.Add(container);
//..............
this.Diagram.Group(string.Empty, true, items.ToArray());
This approach can be used also for the align functionality.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Diagram
Asked by
Barış
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Barış
Top achievements
Rank 1
Share this question
or