开发者

Treeview inheritance relationship

Hallo, I am trying to visualize a hierarchial (Is-a) class relationship using Treeview with WPF but I find it difficult.

I studied some code from the Internet like

http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

These examples show a (has-a) class relationship and in this case the visualization with WPF+Treeview+Databinding is quite clean and intuitive.

On the contrary What I have is

public class Device() { }

public class VisionDevice() : Device { }

public class CommunicationDevice() : Device { }

public class SerialComm() : CommunicationDevice {}

public class Webcam : VisionDevice { }

I would like to visualize a开发者_如何学Go WPF treeview structure like that

Device
    VisionDevice
        WebCam
    CommunicationDevice
        SerialComm

It is possible to do that using HierarchicalDataTemplate but the solution I found is not very elegant.

In conclusion: Treeviews is good for has-a class relationship but difficult to adapt to is-a relationship. Do you agree?


In conclusion: Treeviews is good for has-a class relationship but difficult to adapt to is-a relationship. Do you agree?

Yes, tree views are good for structures where you know the root and have a connection from that to all its children (e.g. an object knows its properties and those properties are in turn objects which behave the same way), for types it is quite problematic since the traveral is the other way around, the supertype knows nothing of its subtypes but the subtypes know their supertypes (unlike objects which rarely have a reference to their parent).

To visualize types you would first need to collect all the leaves and while going towards the root check which types have a branch in common till you arrive at your target supertype. Not very elegant...

(Alternatively you can start at the root and query all types every time to see if their immediate parent is the type you are currently looking at, in terms of performance that should be even worse)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜