Solved

Collection of multiple types

Posted on 2010-09-15
4
880 Views
Last Modified: 2013-12-16
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.  

example:
 if(control.Type == "textbox")
{
            Textbox c = (Textbox)control.GetControl();
             etc.
}
stackPanel.Children.Add(c);

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.

Thanks
0
Comment
Question by:CW596
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Vipul Patel
ID: 33684749
Hi,

Below code might be helpful.

Reason:
System.Windows.UIElement is a base class for most of the objects that have
visual appearance and can process basic input in Silverlight. (From Microsoft code comment)

           
StackPanel stackPanel = new StackPanel();
            List<UIElement> elements = new List<UIElement>();
            elements.Add(new TextBox());
            elements.Add(new TextBlock());
            elements.Add(new StackPanel());

            foreach (UIElement element in elements)
            {
                stackPanel.Children.Add(element);
            }

Open in new window

0
 

Author Comment

by:CW596
ID: 33691962
Hey vrluckyin

This looks like a promising method.  I had a chance to work with it some yesterday for a couple hours before I was side tracked with another project.  Could you give me a few more examples of how you would implement this collection.  Particularly getting the controls out of the collection.  Will I still have to cast into a control?  It would save me some trial and error in getting to a solution.

I will be picking this up later today after I finish some other assignments.  Thanks for your help.
0
 
LVL 5

Accepted Solution

by:
Vipul Patel earned 500 total points
ID: 33695172
Yes, you need to do casting while getting the controls from the collection.
0
 

Author Closing Comment

by:CW596
ID: 33701505
Thanks for the input.  
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question