tags:

views:

30

answers:

1

Hi,
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 listBox 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.

A: 

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.

Enough already
I read it five times, still don't really get your answer :-) The first paragraph is exactly what I want. I try to go away from the list example, read additions above (in 5 min :-)
Christian
Oh, and with my additions, I am not sure how to tell the list to use a UserControl based on Type. I guess that is the actual answer part you meant, but I still don't really get. But its very late here, perhaps tomorrow I will think more clear...
Christian
@Christian: *If* it could work, it would mean you would need to create a custom control, not user control, completed with the miles of associated generic.xml templates. The control would need to evaluate a binding per item to get it's template. I don't even know if this is possible, just throwing ideas out there. Personally, I would take the easy route at the moment: assume the *controller* brains are in a user control used per item in a listbox. That user control displays its contents differently based on the specific data type. I think you have already tried that option though. Good luck.
Enough already