views:

46

answers:

2

I would like to create a UserControl that will have some behavior(animated collapsing...) and some Controls in it (Rectangle, Ellipse, mostly for design purposes).

However I need it to be a valid Panel like Grid so I can put more controls in it when designing main window. Controls that will be add to the new USerControl in main window design time does not need to interact with components that are added to it when creating a UserControl.

How should I do this ? Do I have to extends Control? I'd prefer to extend just UserControl.

+1  A: 

UserControl is a ContentControl which has Content property and usually displays it according on ContentTemplate. The point I'm leading to is that UserControl has 1 item inside of it. Of course you can add Children property to your UserControl and handle adding/removing of sub-items into some internal Grid within your control but it's not what content controls are for.

To host many items you should use a subclass of Panel, like Grid as you mentioned. All panels have Children property with a list of elements arranged on them. You can inherit Grid and place some behavior there. E.g. you can make an extension property MyGrid.Collapsed, set it on elements within your grid, so it can arrange their positions.

Another option is to subclass ItemsControl. Elements placed into it will be displayed according to its ItemsPanel template. This approach also gives you an advantage of ItemsSource property which can be used to display a list of arbitrary data types by generating elements from them using ItemTemplate property.

repka
A: 

What I was looking was ContentPresenter class in combination with dependency properties. More about topic here or here. Or try this google search.

drasto