Problem using MVVM

Hi,

I am having trouble getting a diagram to appear when using a ViewModel rather than a XAML 'code behind' file. The nodes appear but the links do not. I simply bind to a GraphLinksModelNodeData collection and a UniversalLinkData collection using the NodeSource and LinkSource XAML properties as follows:
<go:Diagram NodesSource="{Binding Nodes}" x:Name="myDiagram" LinksSource="{Binding Links}"
The same code works fine when using the XAML code behind file to do the same thing. In this case I instantiate a GraphLinksModel object, assign the model property of the 'myDiagram' XAML object and use the NodesSource and LinkSource properties to add references to the links and nodes collections.
Any ideas?
Regards,
Jon-Paul.

Hi Paul,

Just a blind guess both Nodes & Linkes in VM is not raising PropertyChanged Notification & your DataContext is setted before Nodes & Linkes are created.

Yes, what deep232 suggested is one possibility.

You might want to look at the value of Diagram.LinksSource in the debugger, to make sure it’s what you think it should be. It ought to be the same value as Diagram.Model.LinksSource.

Hi deep232/Walter,

Thanks for replying. Much appreciated.
The property change notification is definately being raised. I actually use the same technique for the links that I use for the nodes and the nodes display fine.
I've also checked both the Diagram.LinksSource and Diagram.Model.Linksource and both are set as expected.
Any other ideas?
Many thanks,
Jon-Paul.

Are you also specifying the Diagram.Model in XAML? If so, I could imagine there’s an issue with setting the Diagram properties in the wrong order. In that case you shouldn’t bind the Diagram.NodesSource or Diagram.LinksSource – instead you should bind the GraphLinksModel.NodesSource and GraphLinksModel.LinksSource directly.

Although I’m not sure how that would account for the LinksSource property values being correct in the debugger.

Hi Walter,

No I'm not specifying the Diagram.Model in XAML. I've basically taken my cue from the example in the documentation and just use go:Diagram as follows:
<go:Diagram NodesSource="{Binding Nodes}" x:Name="myDiagram" LinksSource="{Binding Links}"
How would I specify Diagram.Model instead of this? Perhaps then I could try binding GraphLinksModel.NodesSource as you suggest.
Regards,
Jon-Paul.

Hi,

I'm just looking at the video at http://www.youtube.com/watch?v=WTTepPE6joI&fmt=22 and he appears to be able to incorporate a model tag within his diagram tag , i.e. XAML such as the following:
<go:Diagram> <go:Diagram.Model> <go:UniversalGraphLinksModel>

</go:UniversalGraphLinksModel>
</go:Diagram.Model>
</go:Diagram>

When I try this, I get errors including UniversalGraphLinksModel not found. The intellisense doesn’t pick up anything within the model tag either. I’m wondering if this has anything to do with my problem?

Are you using Silverlight or WPF? If Silverlight, are you using Silverlight 4?

It turns out that Silverlight 3 had a lot of little deficiencies in its XAML support. Most of the problems were fixed when they re-implemented XAML support in Silverlight 4. One of the deficiencies is in the declaration of namespaces.

If you are using Silverlight 4 or any version of WPF, you should declare the GoXam namespace using:

    xmlns:go="http://schemas.nwoods.com/GoXam"

That will allow you to refer to classes in all of the GoXam namespaces using the “go:” XML namespace prefix. And you can remove all of the declarations like:

xmlns:go="clr-namespace:Northwoods.GoXam;assembly=Northwoods.GoSilverlight" xmlns:gomodel="clr-namespace:Northwoods.GoXam.Model;assembly=Northwoods.GoSilverlight" xmlns:gotool="clr-namespace:Northwoods.GoXam.Tool;assembly=Northwoods.GoSilverlight" xmlns:golayout="clr-namespace:Northwoods.GoXam.Layout;assembly=Northwoods.GoSilverlight"
All of the Silverlight samples in the 1.2 kit were written to work with Silverlight 3. In the next kit we have changed the samples sources to assume Silverlight 4, so there will be very few differences with the WPF samples sources.

Hi Walter,

I'm using Silverlight4.
Now that I've changed the namespace declaration as you suggested I seem to be able to use UniveralGraphLinksModel. Previously I was using the following which I took from the Silverlight demos.
xmlns:go="clr-namespace:Northwoods.GoXam;assembly=Northwoods.GoSilverlight"
I'll play around with the XAML now and see if I can solve my original problem.
Many thanks,
Jon-Paul.

Hi Walter,

Now when I try to set the NodeSource and LinkSource properties of UniversalGraphLinksModel it crashes in the InitialiseComponent() method of the View with some internal binding error. Here's a section of my XAML. Any ideas at all?
<go:Diagram.Model> <go:UniversalGraphLinksModel NodesSource="{Binding Nodes}" LinksSource="{Binding Links}"></go:UniversalGraphLinksModel> </go:Diagram.Model>
Cheers,
Jon-Paul.

Sorry about that – I misled you.

Actually, models are not DependencyObjects, so their properties, including NodesSource and LinksSource, cannot be data-bound.

That’s why we added those two properties as dependency properties on Diagram.

Ok thanks. I’m still not sure why I cannot see my links when binding to my viewmodel despite it working fine when I use view ‘code behind’. When I use code behind however I populate the NodeSource and LinkSource properties using the object model rather than binding within XAML.

Any more suggestions would be greatly appreciated.
Cheers,
Jon-Paul.

I just tested this: I modified the Draggable Link sample to declare the Diagram.Model in XAML and having bindings of Diagram.NodesSource and Diagram.LinksSource to an object set as the page’s DataContext with properties named “Nodes” and “Links”. I commented out the code to create the model and initialize its NodesSource and LinksSource properties.

Everything worked fine. So I’m at a loss to explain why nodes are working for you but links are not.

Thanks for spending time looking into this for me. Is there any chance you’d be able to send me the code you used? It would be greatly appreciated.

Cheers,

Jon-Paul.