Solved

Collection of multiple types

Posted on 2010-09-15
4
878 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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