Graph with Single node only

I am creating a proof of concept application where I am trying to create a graph with single node only (without any links) at the time of creation.

I want to show a plus(+) button which I managed to add with the graph. User can add other nodes by clicking the button.
The problem is if there is only single node available then it is not displayed on the graph. The graph remains empty.
Please suggest a solution.
My XAML:

<go:Diagram Grid.Row="1" x:Name="myDiagram" Height="600"<?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Padding="10"

HorizontalContentAlignment="Stretch"

VerticalContentAlignment="Stretch"

NodeTemplate="{StaticResource NodeTemplate}"

LinkTemplate="{StaticResource LinkTemplate}">

<go:Diagram.Layout>

<golayout:LayeredDigraphLayout LayerSpacing="150" />

</go:Diagram.Layout>

</go:Diagram>

My XAML.CS

var model = myDiagram.Model;

myDiagram.StartTransaction("AddNode");

MyNode mainNode = new TermNode(“Some Text Display”,”Unique ID”);

model.AddNode(mainNode);

//

// Conditional/Optional code for adding other nodes and link.

//

// The graph is created only when the other nodes and links are

// added in the graph.

//

myDiagram.CommitTransaction("AddNode");

It would be great help to me if someone provides a sample for creating single node runtime for graph.
Thanks,
Hardik

The IncrementalTree sample happens to do exactly that – start off with a single node.

The DecisionTree sample also does, but it actually starts off with a lot of node data in the model and Nodes in the diagram, and only the root Node is visible.

The code that you have shown so far looks fine to me.
What platform and version are you using? What’s your NodeTemplate?

Thanks for quick reply. <?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Here I want to add node to Graph. I need behaviour similar to incremental tree but in Graph.

My code works perfectly even with the graph when there are 2 nodes and link between them. But the single node is not getting added in graph when there is only one node.

Please check my classes for nodes and link below.

public class MyNode : GraphLinksModelNodeData<String>

{

//

}

public class MyLink : GraphLinksModelLinkData<String, String>

{

//

}

My node template:

<DataTemplate x:Key="NodeTemplate">

<StackPanel Orientation="Vertical" go:Part.SelectionAdorned="True"

go:Node.IsTreeExpanded="True">

<Button Visibility="{Binding Path=Data.ShowButton}" x:Name="myCollapseExpandButton"

Click="CollapseExpandButton_Click" FontSize="8" VerticalAlignment="Top"

Content="+" Width="15" Height="15" />

<go:NodePanel Sizing="Auto" >

<Path go:NodePanel.Figure="Rectangle" Fill="LightGray" />

<TextBlock Text="{Binding Path=Data.MyNodeText}" MinWidth="100" MaxWidth="100" TextWrapping="Wrap" ToolTipService.ToolTip="{Binding Path=Data. MyNodeText }" />

</go:NodePanel>

</StackPanel>

</DataTemplate>

Is there a way where I can send my complete sample for your review?

Thanks a lot,

Hardik

Actually, your MyNode data class looks like it’s missing some properties, because your template data-binds to the ShowButton and MyNodeText properties. Also that doesn’t match the TermNode class that you referred to in your first post. Maybe you pasted the wrong code into this last post?

Hello Walter,
By Single node I mean only and only one node in whole graph. No further nodes as well as no links.
Actually my classes have all the required properties and it works perfectly with multiple nodes but I had removed them in previous post because of its confidential nature.
If you suggest me a way then I can provide you complete sample which is working for multiple nodes but not working for single node. It may help you to analyze it in detail.
Could you also inform:
1. Can we add a single node (Only and only one node) in Graph? Can you provide me such sample?
2. What would happen if we are trying to add only single node in graph's model and do not add any further nodes or links?
Thanks a lot,
Hardik

The IncrementalTree sample only has one node data in the model and only one Node in the Diagram.

I’ll try building a simple app that uses your node template. I’m assuming you’re using Silverlight.

Hi Walter,
Thanks for your support.
Please ensure following requirements with your sample.
1. Use Graph instead of tree: I may have links between any node in any form.
2. Use Silverlight 3: Northwoods.GoSilverlight.dll (version 1.1.3.3)
Please refere this image of my sample which has many nodes and runs properly.
Thanks,
Hardik


<UserControl.Resources>



<go:NodePanel Sizing=“Auto” >


</go:NodePanel>


</UserControl.Resources>

<go:Diagram x:Name=“myDiagram”
Padding=“10”
HorizontalContentAlignment=“Stretch”
VerticalContentAlignment=“Stretch”
NodeTemplate="{StaticResource NodeTemplate}"
>
go:Diagram.Layout
<golayout:LayeredDigraphLayout LayerSpacing=“150” />
</go:Diagram.Layout>
</go:Diagram>

using System;
using System.Windows;
using System.Windows.Controls;
using Northwoods.GoXam.Model;

namespace SilverlightApplication1 {
public partial class SingleNode : UserControl {
public SingleNode() {
InitializeComponent();

  var model = myDiagram.Model;
  myDiagram.StartTransaction("AddNode");
  MyNode mainNode = new MyNode("Some Text Display", "Unique ID");
  model.AddNode(mainNode);
  //
  // Conditional/Optional code for adding other nodes and link.
  //
  // The graph is created only when the other nodes and links are
  // added in the graph.
  //
  myDiagram.CommitTransaction("AddNode");
}

private void CollapseExpandButton_Click(object sender, RoutedEventArgs e) {

}

}

public class MyNode : GraphLinksModelNodeData {
public MyNode() {
this.MyNodeText = “”;
this.ShowButton = Visibility.Visible;
}
public MyNode(String text, String key) {
this.Key = key;
this.MyNodeText = text;
this.ShowButton = Visibility.Visible;
}

public String MyNodeText { get; set; }
public Visibility ShowButton { get; set; }

}
}

The SingleNode.xaml and SingleNode.xaml.cs files above, running with Silverlight3:

Hi Walter,

Thanks for this sample.
I am checking it and inform you in some time.
Regards
Hardik

Hi Walter,

I have done some changes in your code and after changes it has stopped working. I must be doing some mistake and need your help to find it.
Please review my code and follow these steps.

<?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

1. To reproduce Issue:

a. Execute the application

b. Click on the Issue button

2. To check correct behavior:

a. Execute the application

b. Click on Correct button

The reason for using buttons for adding nodes: Actual data will come via WCF services asynchronous based on other selection of other controls on page.

public partial class test : UserControl

{

public test()

{

InitializeComponent();

try

{

var model = new GraphLinksModel<MyNode, String, String, MyLink>();

model.Modifiable = true;

myDiagram.Model = model;

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void CollapseExpandButton_Click(object sender, RoutedEventArgs e)

{

}

private void btnIssue_Click(object sender, RoutedEventArgs e)

{

HideButtons();

var model = myDiagram.Model;

myDiagram.StartTransaction("AddNode");

MyNode mainNode = new MyNode("A", "1");

model.AddNode(mainNode);

myDiagram.CommitTransaction("AddNode");

}

private void btnCorrect_Click(object sender, RoutedEventArgs e)

{

HideButtons();

var model = myDiagram.Model;

myDiagram.StartTransaction("AddNode");

MyNode mainNode = new MyNode("A", "1");

model.AddNode(mainNode);

MyNode nodeB = new MyNode("B", "2");

model.AddNode(nodeB);

model.AddLink(mainNode, null, nodeB, null);

myDiagram.CommitTransaction("AddNode");

}

private void HideButtons()

{

btnCorrect.Visibility = Visibility.Collapsed;

btnIssue.Visibility = Visibility.Collapsed;

}

}

public class MyNode : GraphLinksModelNodeData<String>

{

public MyNode()

{

this.MyNodeText = "";

this.ShowButton = Visibility.Visible;

}

public MyNode(String text, String key)

{

this.Key = key;

this.MyNodeText = text;

this.ShowButton = Visibility.Visible;

}

public String MyNodeText { get; set; }

public Visibility ShowButton { get; set; }

}

public class MyLink : GraphLinksModelLinkData<String, String>

{

public String LabelText

{

get

{

return "Is-A";

}

}

}

.XAML:

<UserControl x:Class="TermGraphViewer.test"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:go="clr-namespace:Northwoods.GoXam;assembly=Northwoods.GoSilverlight"

xmlns:golayout="clr-namespace:Northwoods.GoXam.Layout;assembly=Northwoods.GoSilverlight"

Height="700">

<UserControl.Resources>

<DataTemplate x:Key="NodeTemplate">

<StackPanel Orientation="Vertical" go:Part.SelectionAdorned="True"

go:Node.IsTreeExpanded="True">

<Button Visibility="{Binding Path=Data.ShowButton}" x:Name="myCollapseExpandButton"

Click="CollapseExpandButton_Click" FontSize="8" VerticalAlignment="Top"

Content="+" Width="15" Height="15" />

<go:NodePanel Sizing="Auto">

<Path go:NodePanel.Figure="Rectangle" Fill="LightGray" />

<TextBlock Text="{Binding Path=Data.MyNodeText}" MinWidth="100" MaxWidth="100"

TextWrapping="Wrap" ToolTipService.ToolTip="{Binding Path=Data.MyNodeText }" />

</go:NodePanel>

</StackPanel>

</DataTemplate>

<DataTemplate x:Key="LinkTemplate">

<go:LinkPanel go:Part.SelectionElementName="Path" go:Part.SelectionAdorned="False">

<go:Link.Route>

<go:Route RelinkableFrom="False" RelinkableTo="False" />

</go:Link.Route>

<Path go:LinkPanel.IsLinkShape="True" x:Name="Path" Stroke="Black" StrokeThickness="1" />

<Polyline Stroke="Black" StrokeThickness="1"

Points="0 0 8 4 0 8" go:LinkPanel.Alignment="1 0.5"

go:LinkPanel.Index="-1" go:LinkPanel.Orientation="Along" />

<TextBlock Text="{Binding Path=Data.LabelText, Mode=TwoWay}" go:LinkPanel.Offset="8 8"

FontSize="9" go:Part.TextEditable="False" go:LinkPanel.Orientation="Upright" />

</go:LinkPanel>

</DataTemplate>

</UserControl.Resources>

<Grid x:Name="LayoutRoot" VerticalAlignment="Stretch">

<StackPanel Orientation="Vertical" >

<StackPanel Orientation="Horizontal">

<Button x:Name="btnCorrect" Content="Correct" Click="btnCorrect_Click"></Button>

<Button x:Name="btnIssue" Content="Issue" Click="btnIssue_Click"></Button>

</StackPanel>

<StackPanel VerticalAlignment="Stretch" Height="600" >

<go:Diagram Grid.Row="1" x:Name="myDiagram" Height="600"

Padding="10"

NodeTemplate="{StaticResource NodeTemplate}"

LinkTemplate="{StaticResource LinkTemplate}">

<go:Diagram.Layout>

<golayout:LayeredDigraphLayout LayerSpacing="150" />

</go:Diagram.Layout>

</go:Diagram>

</StackPanel>

</StackPanel>

</Grid>

</UserControl>

That’s an interesting initialization bug. Thanks for reporting it!

For now, I suggest you initialize your model in the following fashion:

  var model = new GraphLinksModel<MyNode, String, String, MyLink>();
  model.Modifiable = true;
  var mainNode = new MyNode("A", "1");
  model.AddNode(mainNode);
  model.RemoveNode(mainNode);
  myDiagram.Model = model;

You still end up with an empty model initially.

Hi Walter,

It workded as expected.
Thanks for your support,
Hardik

Could you try using the DLL in:
GoSilverlight1163.zip

Without that hack of temporarily adding an initial node to the model, of course.

Sorry for the inconvenience.

Hello Walter,

I have tried the new DLL and it is working properly without hack.
Regards
Hardik