简体   繁体   中英

UserControl that is also a Grid in WPF

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 .

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. Eg 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM