Solved

Collection of multiple types

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

20 Experts available now in Live!

Get 1:1 Help Now