开发者

Silverlight Best Practice for modular UserControls / Inheritance

I am kind of stuck in the different approaches to UI design in Silverlight, combined with the aspect of reusability. Assume the following simple example:

A base class ORGANISM has a NAME + AGE

The child class DOG has a FURCOLOR

The child class HUMAN has a JOB

Now I have a list of 30 ORGANISMs, some DOG, some HUMAN.

I can use a li开发者_运维百科stBox to show all items (class names).

I want to create a "basic look" for ORGANISM (e.g. Name at the TOP)

If the data object is a DOG, I want to also list its furcolor.

If it is a HUMAN, I want to also list its job description.

I have tried DataTemplates, UserControls, and other stuff, but I seem to be stuck. There is no TargetType in Silverlight (or am I missing something) and this simple example seems to be very strangely complicated. Styles apply to visual elements only, not sure if they could be based on each other. But it also feels wrong...

My real example is more complex, but you get the idea. A base class has some properties I want to render, with child classes having some more I want to "add" without duplicating XAMl.

Any tips or hints in the right direction?

Chris

[EDIT] To clarify, the problem I have is not limited to lists. Another scenario is a details view, showing either a dog or a human. Most of the details view is identical, so I could use a UserControl with a red border to render Organism with a TextBlock bound to Name. Now below this textblock (but still inside the UserControl) there should be either a job or a furcolor block.

UserControl for ORGANISM

=================Fancy Border===================

Name: Horst

=================Fancy Border===================

BasedOn UserControl for HUMAN

=================Fancy Border===================

Name: Horst

Job: Software Designer

=================Fancy Border===================

BasedOn UserControl for Dog

=================Fancy Border===================

Name: Horst

FurColor: Brown

=================Fancy Border===================

Using all the above in the "parent control" and using visibility settings depending on Types just feels wrong, imagine 100 properties of Human, which then all would be hidden but existing in dog controls.


Interesting question. Given that well-behaved data objects should not know how they are being displayed, you basically want the rendered item template, of each item, to vary based on some interpretation of the data or property of each item (like the type).

Per-item template binding?

If you could use item binding on the source of an item template, then a customer converter would make the decision of what template to display based on rules built into it (acting as a type of controller in an MVC model).

Unfortunately bindings on per-item templates in list controls are evaluated once as a list property, not per item. This implies that a new type of list control is needed to solve your problem. One that allows the choice of item template to be bound to properties on the individual items. I have no idea if this is practical, but it sounds feasible.

Hope this helps in the ideas department. Certainly gives me food for thought as I have similar problems to solve soon.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜