Index: Core/Components/src/Core.Components.GraphSharp.Forms/Extensions/TextBlockExtensions.cs =================================================================== diff -u -r62923c1147ab44c7716d00174a795101affc0178 -rd09f1d9a3ec9133e8a671db7131e5f92de8f0ea4 --- Core/Components/src/Core.Components.GraphSharp.Forms/Extensions/TextBlockExtensions.cs (.../TextBlockExtensions.cs) (revision 62923c1147ab44c7716d00174a795101affc0178) +++ Core/Components/src/Core.Components.GraphSharp.Forms/Extensions/TextBlockExtensions.cs (.../TextBlockExtensions.cs) (revision d09f1d9a3ec9133e8a671db7131e5f92de8f0ea4) @@ -46,12 +46,17 @@ private static void FormattedTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs) { - var value = eventArgs.NewValue as string; var textBlock = sender as TextBlock; + if (textBlock == null) + { + return; + } - if (textBlock != null) + textBlock.Inlines.Clear(); + + var value = eventArgs.NewValue as string; + if (value != null) { - textBlock.Inlines.Clear(); textBlock.Inlines.Add(Convert(value)); } } Index: Core/Components/src/Core.Components.GraphSharp.Forms/PointedTreeGraphControl.cs =================================================================== diff -u -r672857a9fcd0dfdcaec314524d580af98894536f -rd09f1d9a3ec9133e8a671db7131e5f92de8f0ea4 --- Core/Components/src/Core.Components.GraphSharp.Forms/PointedTreeGraphControl.cs (.../PointedTreeGraphControl.cs) (revision 672857a9fcd0dfdcaec314524d580af98894536f) +++ Core/Components/src/Core.Components.GraphSharp.Forms/PointedTreeGraphControl.cs (.../PointedTreeGraphControl.cs) (revision d09f1d9a3ec9133e8a671db7131e5f92de8f0ea4) @@ -42,7 +42,6 @@ { private readonly List drawnGraphNodeList = new List(); private ZoomControl zoomControl; - private PointedTreeGraphLayout graphLayout; private GraphNode data; private PointedTreeGraph graph; @@ -54,7 +53,7 @@ public PointedTreeGraphControl() { InitializeComponent(); - InitializeGraph(); + InitializeZoomControl(); } public GraphNode Data @@ -66,6 +65,7 @@ set { ClearData(); + CreateNewGraph(); data = value; @@ -88,7 +88,7 @@ base.Dispose(disposing); } - private void InitializeGraph() + private void InitializeZoomControl() { zoomControl = new ZoomControl { @@ -97,12 +97,6 @@ ZoomDeltaMultiplier = 300 }; - graphLayout = new PointedTreeGraphLayout(); - - graph = new PointedTreeGraph(); - graphLayout.Graph = graph; - - zoomControl.Content = graphLayout; zoomControl.PreviewMouseWheel += ZoomControl_MouseWheel; var myResourceDictionary = new ResourceDictionary @@ -111,6 +105,8 @@ }; zoomControl.Resources.MergedDictionaries.Add(myResourceDictionary); wpfElementHost.Child = zoomControl; + + CreateNewGraph(); } private void ClearData() @@ -120,10 +116,18 @@ drawnGraphNode.Vertex.PropertyChanged -= VertexOnPropertyChanged; } - graph.Clear(); drawnGraphNodeList.Clear(); } + private void CreateNewGraph() + { + graph = new PointedTreeGraph(); + zoomControl.Content = new PointedTreeGraphLayout + { + Graph = graph + }; + } + private void DrawNode(GraphNode node, PointedTreeElementVertex parentVertex = null) { PointedTreeElementVertex vertex = GraphNodeConverter.Convert(node); @@ -177,6 +181,22 @@ SelectionChanged?.Invoke(this, e); } + /// + /// Lookup class for administration related to drawn vertices. + /// + private class DrawnGraphNode + { + /// + /// The graph node which the drawn is based upon. + /// + public GraphNode GraphNode { get; set; } + + /// + /// The drawn vertex. + /// + public PointedTreeElementVertex Vertex { get; set; } + } + #region Zooming private void ZoomControl_MouseWheel(object sender, MouseWheelEventArgs e) @@ -207,21 +227,5 @@ } #endregion - - /// - /// Lookup class for administration related to drawn vertices. - /// - private class DrawnGraphNode - { - /// - /// The graph node which the drawn is based upon. - /// - public GraphNode GraphNode { get; set; } - - /// - /// The drawn vertex. - /// - public PointedTreeElementVertex Vertex { get; set; } - } } } \ No newline at end of file Index: Core/Components/test/Core.Components.GraphSharp.Forms.Test/Extensions/TextBlockExtensionsTest.cs =================================================================== diff -u -r62923c1147ab44c7716d00174a795101affc0178 -rd09f1d9a3ec9133e8a671db7131e5f92de8f0ea4 --- Core/Components/test/Core.Components.GraphSharp.Forms.Test/Extensions/TextBlockExtensionsTest.cs (.../TextBlockExtensionsTest.cs) (revision 62923c1147ab44c7716d00174a795101affc0178) +++ Core/Components/test/Core.Components.GraphSharp.Forms.Test/Extensions/TextBlockExtensionsTest.cs (.../TextBlockExtensionsTest.cs) (revision d09f1d9a3ec9133e8a671db7131e5f92de8f0ea4) @@ -126,6 +126,31 @@ [Test] [Apartment(ApartmentState.STA)] + public void GivenTextBlockWithXml_WhenNewTextXmlNull_ThenInlinesCleared() + { + // Given + const string xmlToConvert = "test"; + var textBlock = new TextBlock(); + + TextBlockExtensions.SetFormattedText(textBlock, xmlToConvert); + + // Precondition + Assert.AreEqual(1, textBlock.Inlines.Count); + var span = (Span) textBlock.Inlines.First(); + + Assert.AreEqual(1, span.Inlines.Count); + var run = (Run) span.Inlines.First(); + Assert.AreEqual("test", run.Text); + + // When + TextBlockExtensions.SetFormattedText(textBlock, null); + + // Then + CollectionAssert.IsEmpty(textBlock.Inlines); + } + + [Test] + [Apartment(ApartmentState.STA)] public void GetFormattedText_DefaultValue_ReturnNull() { // Setup @@ -136,7 +161,6 @@ // Assert Assert.IsNull(formattedText); - } [Test] @@ -146,7 +170,7 @@ // Setup const string xmlToConvert = "test bold text"; var textBlock = new TextBlock(); - + TextBlockExtensions.SetFormattedText(textBlock, xmlToConvert); // Call Index: Core/Components/test/Core.Components.GraphSharp.Forms.Test/PointedTreeGraphControlTest.cs =================================================================== diff -u -r616979c00d24e9ad678e1c945a15e3c38fa49391 -rd09f1d9a3ec9133e8a671db7131e5f92de8f0ea4 --- Core/Components/test/Core.Components.GraphSharp.Forms.Test/PointedTreeGraphControlTest.cs (.../PointedTreeGraphControlTest.cs) (revision 616979c00d24e9ad678e1c945a15e3c38fa49391) +++ Core/Components/test/Core.Components.GraphSharp.Forms.Test/PointedTreeGraphControlTest.cs (.../PointedTreeGraphControlTest.cs) (revision d09f1d9a3ec9133e8a671db7131e5f92de8f0ea4) @@ -86,14 +86,17 @@ }, false) }, false); + PointedTreeGraph originalGraph = PointedTreeGraphControlHelper.GetPointedTreeGraph(graphControl); + // When graphControl.Data = node; // Then - PointedTreeGraph graph = PointedTreeGraphControlHelper.GetPointedTreeGraph(graphControl); + PointedTreeGraph newGraph = PointedTreeGraphControlHelper.GetPointedTreeGraph(graphControl); - Assert.AreEqual(5, graph.VertexCount); - Assert.AreEqual(4, graph.EdgeCount); + Assert.AreNotSame(originalGraph, newGraph); + Assert.AreEqual(5, newGraph.VertexCount); + Assert.AreEqual(4, newGraph.EdgeCount); } } @@ -121,6 +124,7 @@ graphControl.Data = new GraphNode("Double used", new GraphNode[0], false); // Then + graph = PointedTreeGraphControlHelper.GetPointedTreeGraph(graphControl); Assert.AreEqual(1, graph.VertexCount); Assert.AreEqual(0, graph.EdgeCount); } @@ -150,6 +154,7 @@ graphControl.Data = null; // Then + graph = PointedTreeGraphControlHelper.GetPointedTreeGraph(graphControl); Assert.AreEqual(0, graph.VertexCount); Assert.AreEqual(0, graph.EdgeCount); }