Now, lets turn to WPF and let me give you some further ideas before we close this discussion and you continue on your own. I hope you will choose to accept this one formally, too. (You can click green "Accept" button on more than one.)
With WPF, you can put all your diagram elements on the surface of
Canvas
, please see
http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx[
^].
The children of the
Canvas
are of the compile-time of
UIElement
, and the
Canvas.Childen
collection is
polymorphous. This base class
UIElement
is quite enough to add all kinds of input events to its real-time instances, to implement all manipulations you need for editing. Please see
http://msdn.microsoft.com/en-us/library/system.windows.uielement.aspx[
^].
Unlike controls, the WPF elements can freely overlap, be semi-transparent and a lot more. The connectors will also be the children of your
Canvas
, peer-to-peer with the connected elements. (Not having this arrangement was actually the major problem you expressed in your original post.)
One special advanced advice about it: you can even allow the users to add their custom elements to the diagram. Here is the idea: the user can create their own vector graphics elements of any degree of complexity and save them in XAML format. Your application can easily load them during run-time and add to the canvas. You can dynamically attach any properties to them during run time.
One great way of developing of such vector images the the brilliant Open Source vector editor Inkscape. Please see:
http://en.wikipedia.org/wiki/Inkscape[
^],
http://inkscape.org/[
^].
You can draw vector graphics in its native SVG format (
http://en.wikipedia.org/wiki/Scalable_Vector_Graphics[
^]) and export as XAML. You should remember that SVG is richer then XAML, so you could not use some advanced SVG properties such as fuzziness. At the same time, the properties you can export to XAML are quite reach, including gradients, transparency and a lot more. The gallery of available samples is really amazing.
You can solve the problem of zooming and panning of your diagram by putting your whole
Canvas
in the
Viewbox
, see
http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox.aspx[
^].
Now, you need to create a sound architecture of the system which should define the layers and interaction between them: database, other persistence, exports, loading and edition, all properly synchronized.
First of all, you need to develop a
data model of a diagram. Try to segregate the model in two parts: semantic and graphical. At least semantic part of the model should be agnostic to the graphical. Semantic part of the model should model the semantic aspects of the diagram, the application field modeled by the diagram and agnostic to the diagram itself, such aspects as shapes and colors. It should only be concerned with the semantic properties of objects modeled by a diagram and semantic relationships.
This data model should present a separate layer of the architecture. Other layers should communicate only through this model. For example, input events should not deal with database or even with the canvas children properties, but only with the data model. You should poll database and update data model first, and only the graphics can be updated only from the data model. It will provide adequate level of abstraction. For example, later on you will be able to replace your database and mapping code, and it will be relatively easy because only two layers are involved.
You may need to export all the diagram in a local file or sent over the network stream. To do that, use only one thing:
Data Contract. This is the most reliable, flexible and non-intrusive way. You have to make your data model a set of pure data classes. To make it a data contract, you only need to add some attributes to types and members. What is very important: you
object graph can be an arbitrary graph, not a tree. Data Contract serializers will handle it. You also can organize perfect backup compatibility needed to upgrade the data model. You file type can be made world unique, as you can (and you should) specify your unique (say, company-based) namespace to the meta-data.
See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[
^]
See also my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[
^],
Creating property files...[
^],
deseralize a json string array[
^].
Now, let's get back to the layered architecture. I suggest you learn and analyze applicability of the following
architectural patterns (
http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[
^]):
MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],
MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),
MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],
MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create robust architecture for this application.
—SA