Add Shape - Position on Mouse Position

4 posts, 0 answers
  1. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 07 Aug 2014 Link to this post

    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
    {
    ...
    }

  2. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 07 Aug 2014 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 12 Aug 2014 Link to this post

    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.
     
  5. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 12 Aug 2014 Link to this post

    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.
Back to Top