Solved

Collection of multiple types

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now