Mvvm

I am playing with GoXam Silverlight 4 and it seems we will move on to GoXam very soon. I start look at NodeFigures example, since it is the easiest one (I think).

  • I would like to apply MVVM to my application, extract the business logic to the ViewMode class without writing any code in the xxx.xaml.cs file. Please point me how to set the following int Xaml instead of in the code behind file.

myDiagram.Model.NodesSource = figures;
myDiagram.Model.Modifiable = false;

Here is my ModelView class
namespace MyApps.NodeFigures
{
public partial class NodeFigures : UserControl
{
public NodeFigures()
{
InitializeComponent();
var figures = new List();
var i = 0;
while (Enum.IsDefined(typeof(NodeFigure), i))
{
figures.Add((NodeFigure)i++);
}
myDiagram.Model.NodesSource = figures;
myDiagram.Model.Modifiable = false;
}
}
}
  • We will create a node with an Image and serveral texts below the image; the node is resizable. For example, a border around the whole node, resize the border, the image and texts will be resized. So I added a grid in NodeFigureTemplate, unfortunately the figure disappear. How can I display both figure and text?


<go:NodePanel go:Node.SelectionElementName="Shape" go:Node.SelectionAdorned="True"
go:Node.Resizable="True" go:Part.Text="{Binding Path=Data}">






<Path x:Name="Shape" Grid.Row="0" go:NodePanel.Figure="{Binding Path=Data}"
Stroke="Black" StrokeThickness="3" Fill="LightGray"
Width="100" Height="100" MinWidth="20" MinHeight="20" />
<TextBlock Grid.Row="1" Text="{Binding Path=Data}" TextAlignment="Center" TextWrapping="Wrap"
VerticalAlignment="Center" HorizontalAlignment="Center" />


I’m pretty sure there’s no way to find a collection of enum values in XAML. It has to be done in code, somewhere. If you don’t want it in the code-behind for the UserControl, that’s OK.

For example, you could define your own model class that initializes the data:

public class MyModel : GraphModel<NodeFigure, NodeFigure> { public MyModel() { #if SILVERLIGHT // no Enum.GetValues List<NodeFigure> figs = new List<NodeFigure>(); int i = 0; while (Enum.IsDefined(typeof(NodeFigure), i)) { figs.Add((NodeFigure)i); i++; } this.NodesSource = figs; #else this.NodesSource = Enum.GetValues(typeof(NodeFigure)); #endif } }
Then you can write the XAML:

<go:Diagram . . .> . . . <go:Diagram.Model> <local:MyModel /> </go:Diagram.Model> </go:Diagram>

The problem with your NodeFigureTemplate is that the <Path … go:NodePanel.Figure="…" /> isn’t a direct child of the go:NodePanel element.

I suggest you use the as the root visual element in the template. You’ll need to move those go:Node attached properties from the NodePanel to the Grid.

Walter,

Thank you for your quick response.