Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Collection of multiple types

Posted on 2010-09-15
4
Medium Priority
?
882 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …
Suggested Courses

670 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