Is it possible to set up event handlers on the parts of the drawing, which is obtained with DrawingImage? For example, I make the graph drawing (mathematical graph with vertices and edges), graph is loaded from txt file, and I want to make vertices movable (with incident edges) but I don't know how to do that, since I've made a drawing with DrawingImage. Any suggestions?
Asked
Active
Viewed 139 times
0
-
You don't "draw" stuff in WPF. You create the proper data and let the UI "draw" itself. Post a screenshot of what you need and I can tell you the proper way to do it in WPF. – Federico Berasategui Apr 19 '13 at 14:11
-
Start reading [WPF Graphics Rendering Overview](http://msdn.microsoft.com/en-us/library/ms748373.aspx) and [Drawing Objects Overview](http://msdn.microsoft.com/en-us/library/ms751619.aspx) and [Shapes and Basic Drawing in WPF Overview](http://msdn.microsoft.com/en-us/library/ms747393.aspx). – Clemens Apr 19 '13 at 16:40
-
This is a screenshot of graph with 40 nodes. You can see there are some crossing edges, which doesn't look nice. I want to make nodes moveable, so user can make a better picture for himself, because the real program will have much more nodes then this graph and more crossing edges. http://tinypic.com/r/2dl9mhw/5 @HighCore – Daisy Apr 26 '13 at 08:18
1 Answers
0
That really looks like this, except I took a completely different approach to it.
The default approach to WPF is something called MVVM, where, as I mentioned, you don't "tell the UI to do things", but instead you define the proper data, and use DataBinding to let the UI "draw itself".

Community
- 1
- 1

Federico Berasategui
- 43,562
- 11
- 100
- 154
-
Thank you very much for the answer. I've been doing some research about MVVM pattern. Unfortunately I can't run your example to see how it works.. I have this exception: Service provider is missing the INameResolver service. Can you tell me how to solve this, please? – Daisy May 04 '13 at 15:34
-
@Daisy I never ever saw that before. Can you show me the full exception with the stack trace etc? – Federico Berasategui May 06 '13 at 15:43
-
WOW! I just started your project somehow and it's great, it may help me a lot! But, when I open the MainWindow.xaml I still have the two errors which are the same. Except the above description of exception, there are no further information. It happens at line 32, 33 on the {x:Reference view spot (
). – Daisy May 08 '13 at 19:56 -
Also, when I try to open your project, first, I have this:The associated source control plug-in is not installed or could not be initialized. Common causes for this error include server unavailability and/or incorrect workspace mappings. – Daisy May 08 '13 at 19:57
-
@Daisy the source control thing is normal, the other one I don't know. – Federico Berasategui May 08 '13 at 20:11
-
I found full exception with the stack trace, I would divide it into two comments because it's too long: at System.Windows.Markup.Reference.ProvideValue(IServiceProvider serviceProvider) at Microsoft.Expression.DesignModel.Core.InstanceBuilderOperations.SetValue(Object target, IProperty propertyKey, Object value) at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.ModifyValue(IInstanceBuilderContext context, ViewNode target, IProperty propertyKey, Object value, PropertyModification modification) at – Daisy May 09 '13 at 09:03
-
Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.UpdateProperty(IInstanceBuilderContext context, ViewNode viewNode, IProperty propertyKey, DocumentNode valueNode) – Daisy May 09 '13 at 09:08
-
I have one more question about your project. Is it necessarily to use ObservableCollection (I know that is because of MVVM pattern)? In my project, data is represented with Dictionary, so I don't know how to adjust it...do I have to change the whole concept of data representation? - And, how did you make a background table with squares where nodes getting moved, and implemented the "moving" part? Can it be invisible for user? – Daisy May 09 '13 at 11:20
-
@Daisy What do you use dictionaries for? put relevant data in the `Node` class instead, and use `ObservableCollection`s. I don't like dictionaries. – Federico Berasategui May 09 '13 at 13:39
-
The electrical elements (nodes and edges in graph) are represented with dictionaries because they all have ID as a key. – Daisy May 10 '13 at 09:18
-
I still don't know how to solve that error. Can this be coded differently?
Something's wrong with Source={x:Reference view} part. – Daisy May 16 '13 at 11:03 -
@Daisy are you compiling against full .Net Framework 4.0? That should not be a problem. I have the project running here. Are you using Visual Studio 2010 or 2012? My environment is Visual Studio 2010 SP1 on Windows 7. I don't know if that could cause a problem? – Federico Berasategui May 16 '13 at 14:58
-
@Daisy the background is just a `VisualBrush`, if you don't want it, just remove it from the XAML. The movement is performed by the `Thumbs` and the "grid snapping" occurs in the Model (the `Node` Class) where the `X` and `Y` values are always forced to multiples of 50. So if you `Node.X = 35;` then the value will be forced to `Node.X == 50`; – Federico Berasategui May 16 '13 at 15:03
-
@Daisy With regards to dictionaries and stuff. Your entity Model should be self-sufficient in the sense that a single class should contain all the relevant information to be able to be saved into a database (without depending on dictionaries of any sort). So if you need the `Nodes` to have and Id, then put a `public int Id {get;set;}` in the `Node` class instead. Don't use dictionaries for data representation. – Federico Berasategui May 16 '13 at 15:05
-
My version is 4.0.30319 RTMRel, 2010, and I found out the problem is with VS - http://xstatic2.wordpress.com/2011/10/06/xreference/#comment-15 . Is there any other way to concatenate the Nodes and Connectors in a single one? – Daisy May 21 '13 at 13:31
-
@Daisy I guess you can do it at the ViewModel level, by creating an `ObservableCollection
` which is the base class of Nodes and Connectors, but you will have to listen to `CollectionChanged` events in the Nodes and Connectors Collection and add these to the "Merged" collection as well. – Federico Berasategui May 21 '13 at 14:35