RadDiagram.Load(xml) overrides my Style

0 Answers 27 Views
Diagram
Dan
Top achievements
Rank 1
Iron
Iron
Dan asked on 27 Oct 2023, 08:55 AM

I have this code in xaml:

 <telerik:RadDiagram
     x:Name="RadDiagram"
     primitives:BackgroundGrid.LineStroke="{DynamicResource SelmoBrushDiagramLineStroke}"
    ..../>

The problem is before the colors were predefined. It was a hex value.

Everytime I load the digram xml  with RadDiagram.Load(xml) the LineStroke has the hex color which was saved before in the serialzed xml and the new Dynamic Color doesn't get applied.

Is there any possibility to override the loaded diagram xml, or maybe set the the LineStroke in codebehind after loading the diagram xml?

Dinko
Telerik team
commented on 31 Oct 2023, 05:19 PM

Hello Dan,

May I ask if you can check the events that occur when a serialization/deserialization process is executed in the RadDiagram control? More information on the raised events can be found at the following link:

WPF Diagram - Diagram Events - Telerik UI for WPF

Dan
Top achievements
Rank 1
Iron
Iron
commented on 02 Nov 2023, 02:23 PM

I'm only using Load() and Save(). The problem is that before the diagrams were saved in the xml format with the previous LineStroke color, now I want to override this color and save the diagram xml with the new color from the DynamicResource.
Dinko
Telerik team
commented on 03 Nov 2023, 02:22 PM

Hello Dan,

To achieve the desired requirement, you can subscribe to the Deserialized event of the RadDiagram control. The Deserialized event is raised when the RadDiagram has finished deserializing its content. In other words, it occurs after the process of loading a previously saved diagram state from the XML representation.

<telerik:RadDiagram x:Name="RadDiagram"
                    Deserialized="RadDiagram_Deserialized" />

In the code behind you can handle the Deserialized event and change the LineStroke of the RadDiagram.

private void RadDiagram_Deserialized(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
	BackgroundGrid.SetLineStroke(this.RadDiagram, new SolidColorBrush(Colors.Red));
}

I am using the SetLineStroke method which comes from the BackgroundGrid class to set the LineStroke of the RadDiagram to new SolidColorBrush color.

For your convenience I prepared a sample solution where this behavior is implemented.

Petar Mladenov
Telerik team
commented on 03 Nov 2023, 03:51 PM

Hi Dan,
Let me add more information regarding the case, on top of the provided workaround which locally overrides the attached property in question.
On Load, the deserialization logic of the Diagram locally sets some properties if they are saved in the XML. Тhe local set value overrides and breaks the binding for the current runtime of the application. To prevent this local set, for shapes and connections in MVVM scenario you can use the following approach:
   Diagram Preserve Bindings to properties
However, for diagram properties, the closest possible approach is to inherit the Diagram and override the Deserialize() method :

    public class CustomDiagram : RadDiagram
    {
        public override void Deserialize(SerializationInfo info)
        {
            // this will avoid local setting during base.Deserialize();
            info[SerializationConstants.BackgroundGridLineStroke] = null;

            base.Deserialize(info);
        }
    }
I hope you can use this successfully at your side in order to preserve the binding.

No answers yet. Maybe you can help?

Tags
Diagram
Asked by
Dan
Top achievements
Rank 1
Iron
Iron
Share this question
or