Collection of multiple types
Posted on 2010-09-15
I have a Silverlight 4 stack panel that I have been populating with textblock controls that are stored in a List collection. List<Textblock> controls = new List<TextBlock>();
I want to be able to populate the stack panel dynamically with any control I want. I am thinking that I will have to go to an arraylist or create a Control Type class that can store the properties for the controls in each instance. i.e. List<ControlType> controls ... The Control Type class would return an object type that would have to be case into a specific control based on a property stored in the Control Type class.
if(control.Type == "textbox")
Textbox c = (Textbox)control.GetControl();
Either way I go I will probably have to cast into a new control type when I pull the object out of the collection. I was just wondering if anyone had any creative solutions to this problem or if I am missing something. I don't have any real problems with arraylists but if I could avoid the need to cast an object it would be a huge performance benefit because this is for a very heavy data driven application. It is basically a custom data grid that I am creating. If there is a better set of controls to use to handle this I am interested in hearing about it. The more flexible the solution the better.
I am hoping to get some cool ideas from you folks.