开发者

Selector that can only take child elements of a certain type?

开发者 https://www.devze.com 2022-12-20 16:54 出处:网络
I need to write a control that is supposed to take only a certain type of child controls. Functionally it works like a Selector in that I can select/activate on of its childs but it seems that I can n

I need to write a control that is supposed to take only a certain type of child controls. Functionally it works like a Selector in that I can select/activate on of its childs but it seems that I can not derive from Selector because an ItemsControl can take any type of child (object). I really would like to have compiletime typesafety here. So throwing an exception when a child of the wrong type is added is not an option.

This constraint seems to be necessary because the parent control needs to rely on certain properties and behavoir in its children. There is also some direct communication of the children with its parents.

How is something like this usually开发者_如何学Go handled in WPF.


ItemsControl's wrap their children in an item container type of your choice. Thus, you know the children of your ItemsControl will always be of that type. For example, ListBoxes always have ListBoxItems as children.

0

精彩评论

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

关注公众号