RadDiagram MVVM + UndoRedo

4 posts, 0 answers
  1. Kamil
    Kamil avatar
    1 posts
    Member since:
    Apr 2011

    Posted 13 Dec 2013 Link to this post

    Hi,

    Consider my scenario 

    - I added items to graph from my custom toolbox (not telerik), i implemented ICommnad, and execute under UndoRedoService
    - Moving newly added item on graph surface - OK
    - Pressing Ctrl-Z until element disappear(Undo on my custom Command) - position of element changed properly -  OK
    - Pressing Ctrl-Y - element appear - OK
    - Pressing Ctrl-Y - element should move, but it stays in same position.. - BUG

    To clarify,  further pressing Ctrl-Z/Y has no impact on element position
    public void Undo(object a_state)
    {
       if (m_createdNode == null)
       {
          return;
       }
     
       GraphSource.RemoveItem(m_createdNode);
    }

    public void Redo()
    {
       if (m_createdNode == null)
       {
          return;
       }
     
       GraphSource.AddNode(m_createdNode);
    }

    Any help appreciated.
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1183 posts

    Posted 18 Dec 2013 Link to this post

    Hello Kamil,

    I am not sure that I fully understand your custom scenario. I am not sure where the provided code is implemented and what exactly you expect to happen when Ctrl+Z/Y combination is pressed. Also could you please elaborate on the actions that you need to Undo/Redo. You talk about changing the position, but the provided code handles the Removing/Adding items, only. It will be great if you can reproduce the reported behavior in a small test project and send it over. By doing so we will be able to fully understand your scenario and investigate the reasons behind the issue.

    For your convenience and demonstration purposes I created a sample solution showing how the UndoRedo service can be used. Please take a look at it and let us know if you have any other questions.

    Regards,
    Pavel R. Pavlov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    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. DevCraft banner
  4. Barış
    Barış avatar
    19 posts
    Member since:
    Jul 2015

    Posted 16 Oct 2015 in reply to Pavel R. Pavlov Link to this post

    Hello Pavel,

    There seems I also suffer from save behavior. I implemented my graph source. I call AddLink and link appears. Then I click "Undo" and link disappears. And finally if I click "Redo", I expect to link reappears but it doesn't. Because in AddLink function, I check if the target and source property is not null. But in that case, after "Redo", AddLink is triggered, but Target is null. Actually target is set to null even in the RemoveLink function. Can you please help me to solve this.

     

    /// <summary>
    /// Adds the link.
    /// </summary>
    /// <param name="link">
    /// The link.
    /// </param>
    public override void AddLink(Link<T> link)
    {
        if (link == null)
        {
            throw new ArgumentNullException("link");
        }
     
        if (link.Source != null)
        {
            link.Source = this.GetNode(link.Source.Entity.GetIdentifier());
        }
     
        if (link.Target != null)
        {
            link.Target = this.GetNode(link.Target.Entity.GetIdentifier());
        }
     
        base.AddLink(link);
     
        if (link.Source != null && link.Target != null)
        {
            link.Source.ChildrenCount++;
            link.Target.ParentCount++;
     
            var foundLink =
                this.InternalLinks.FirstOrDefault(
                    x =>
                    (IsSameNode(x.Source, link.Source) && IsSameNode(x.Target, link.Target))
                    || (IsSameNode(x.Target, link.Source) && IsSameNode(x.Source, link.Target)));
            if (foundLink == null)
            {
                this.RemoveLink(link);
            }
        }
        else
        {
            this.RemoveLink(link);
        }
    }
    /// <summary>
    /// Removes the link.
    /// </summary>
    /// <param name="link">
    /// The link.
    /// </param>
    /// <returns>
    /// The operation result.
    /// </returns>
    public override bool RemoveLink(Link<T> link)
    {
        if (link == null)
        {
            throw new ArgumentNullException("link");
        }
     
        bool result = base.RemoveLink(link);
        if (result)
        {
            if (link.Source != null)
            {
                link.Source.ChildrenCount--;
            }
     
            if (link.Target != null)
            {
                link.Target.ParentCount--;
            }
        }
     
        return result;
    }

         

  5. Milena
    Admin
    Milena avatar
    204 posts

    Posted 21 Oct 2015 Link to this post

    Hello Barış, 

    Thank you for the code-snippet. 

    When you add connection (Link) the diagram executes 2 undoable actions  - create a connection and then change its target. In this case when you redo this user action (connect shape1 to shape2), in the overrided AddLink method the target is not yet set. If you need to get the Target, you can subscribe to PropertyChanged event of the link: 
    public override bool AddLink(Link link)
    {
    if (link.Target == null)
    {
      link.PropertyChanged += link_PropertyChanged;
    }
    ....
    }


    void link_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "Target")
        {
           // GetIdentifier
        };
    }

    The same applies to RemoveLink. 

    I hope this information helps.

    Regards,
    Milena
    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
Back to Top