RadDiagram: (De-)Serialization of the position of a shape in a Databound diagram

1 Answer 47 Views
Diagram
Peter
Top achievements
Rank 1
Peter asked on 14 Apr 2022, 08:58 PM

(I sincerely hope this is not a duplicate and I'm not stating the question too convolutedly.)

In your Diagram/MVVM example you demonstrate binding a RadDiagram to a ViewModel including serialization and deserialization with two ViewModel classes derived from NodeViewModelBase (Model, Brand) associated with the diagram's shapes.

The XAML for the diagram defines custom styles for RadDiagramShape and RadDiagramContainerShape which (as explained in your How-To page on StyleSelectors and MVVM) bind their Position dependency property to the (derived, via NodeViewModelBase) Position property of the ViewModel classes.

<Setter Property="Position" Value="{Binding Position, Mode=TwoWay}" />

When the graph is constructed the shapes' positions are indeed taken from the ViewModel classes and when the graph is serialized, so are the positions. The serialized position values can, as expected, be found in the generated XML file.

However, when the graph is deserialized, these values are not retrieved in the DeserializeNode override (see below), neither via a call to NodeViewModelBase.DeserializeNode nor by explicitly querying the "Position" attribute in the XML. The Position property of these NodeViewModelBase-derived objects is still (0,0) upon exit. (Only the (inherited) Content property of the node-related ViewModel classes (Model, Brand) is set from "Content" XML attribute.)

Excerpt from CarsGraphSource.cs:

public override NodeViewModelBase DeserializeNode(IShape shape, Telerik.Windows.Diagrams.Core.SerializationInfo info)
{
    NodeViewModelBase node = null;
    if (shape is IContainerShape)
    {
        node = new Brand();
    }
    else
    {
        node = new Model();
    }
    if (info["Content"] != null)
        node.Content = info["Content"].ToString();
    if (info[this.NodeUniqueIdKey] != null)
    {
        var nodeUniquekey = info[this.NodeUniqueIdKey].ToString();
        this.CachedNodes[nodeUniquekey] = node;
    }

    return node;
}

Still, after deserialization, the position of the shapes is restored correctly (despite the Position property of the ViewModel classes still being (0,0).

Am I understanding something completely wrong here? Thank you very much in advance for your help.

1 Answer, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 18 Apr 2022, 09:05 AM

Hello Peter,

Thank you for the provided code snippets and references.

The reason for this behavior is that some of the properties of the RadDiagram are serialized automatically. The list of these properties contains the Position property and can be found here: Extending RadDiagram Serialization. You can, however, still decide to manually serialize/deserialize the property. Please keep in mind that In this case, to avoid issues, it is recommended to use a custom key for these properties which differs from the property's default name.

You can also refer to the following section of the article for more information about how to preserve the binding to the Position property after deserialization takes place: Preserve bindings to the automatically serialized properties.

I hope you find these resources helpful.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Peter
Top achievements
Rank 1
commented on 21 Apr 2022, 08:21 AM

This is probably not the proper place but I would nevertheless like to take the opportunity to thank you very, very much for your kind and informative answer. Telerik has a consistently outstanding record regarding the development of UI technologies and its excellent support certainly contributes to that! Thanks a lot!

Dilyan Traykov
Telerik team
commented on 21 Apr 2022, 02:14 PM

Hello Peter,

Thank you very much for the kind words. Our customers' satisfaction is what keeps us motivated to provide you with the best services possible. If you require any further assistance, we will be more than happy to assist you again.
Tags
Diagram
Asked by
Peter
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or