문제

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top