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

Add Shape - Position on Mouse Position

3 Answers 167 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Joel Palmer
Top achievements
Rank 2
Joel Palmer asked on 07 Aug 2014, 07:04 PM
When I press a button to add a shape to my canvas I need it to be placed inside my current viewing area on the canvas.  Currently, I have it hard-coded.  Do you have some example code to do this?  I pass the position using my "GroupDD" object.

Thanks in advance for your help,
Joel

private void btnAddGroup_Click(
    object sender,
    RoutedEventArgs e)
{
    try
    {
        ((AppSettings)DataContext).Model.AddGroup(
            new GroupDD()
            {
                Name = "New Group",
                DynamicProperties = new NameValuePairList()
                {
                    new NameValuePair()
                    {
                        Name = "PositionLabel",
                        Value = GroupNode.GetPointLabel(
                            new Point(20, 20))
                    }
                }
            });



        
public void AddGroup(
            GroupDD group)
        {
            GroupNode node = GetGroupNode(group);
            GraphSource.AddNode(node);
        }
 
        public GroupNode GetGroupNode(
            GroupDD group)
        {
            GroupNode node = new GroupNode()
            {
                Position = GetPoint(group.DynamicProperties),
                Group = group
            };
 
            return node;
        }
 
public class GroupNode : NodeViewModelBase, INotifyPropertyChanged
{
...
}

3 Answers, 1 is accepted

Sort by
0
Joel Palmer
Top achievements
Rank 2
answered on 07 Aug 2014, 07:05 PM
Sorry for the incorrect title.  Originally, I thought I wanted it on the Mouse Position.  But, because I'm pressing a button my mouse won't be over the canvas.
0
Martin Ivanov
Telerik team
answered on 12 Aug 2014, 12:57 PM
Hello Joel,

I don't think I understand what you mean by saying "Currently, I have it hard-coded". Can you please explain what is hard-coded and where? This will allow me to understand your scenario better.

As for the current viewing area, you can get it with the Viewport property of the RadDiagram. The property contains the current visible area of the diagram. For example if you want to set the shape on the middle of the view port you can use the following code:

private void Button_Click(object sender, RoutedEventArgs e)
{         
    var centerPoint = this.diagram.Viewport.Center();
 
    GroupNode node = new GroupNode()
    {
        Position = centerPoint
    }
    this.model.Source.AddNode(node);
}
You can use the Viewport to calculate where you want to place your shape.

Please let me know if this works for you.

Regards,
Martin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Joel Palmer
Top achievements
Rank 2
answered on 12 Aug 2014, 03:34 PM
This is hard coded (as shown above). 

Value = GroupNode.GetPointLabel(new Point(20, 20))

I believe you gave me what I needed... a relative point.  I haven't worked with the ViewPort so this looks good.  Thanks.
Tags
Diagram
Asked by
Joel Palmer
Top achievements
Rank 2
Answers by
Joel Palmer
Top achievements
Rank 2
Martin Ivanov
Telerik team
Share this question
or