tags:

views:

793

answers:

2

Hey, I was wondering if it was possible to loop through several WPF controls. So lets say I have a control that I want rendered (with different data each time) 10-20 times depending on what it has.

The only alternative I found was to manually create 20 of these user controls and hide the ones I was not using.

Is it possible to loop them?

+3  A: 

You're probably going to have to refine your question.

What do you mean by "loop through?"

When you say you want to render a control "10-20 times", do you mean you have one control you need to change 10-20 times or 10-20 controls?

You might be interested in looking into DataTemplates and ItemsControls. You can bind the ItemsControl to an ObservableCollection containing different types of objects, then define different DataTemplates for each type of object contained in the collection. The ItemsControl will render the appropriate control for each object contained in the ObservableCollection.

Will
Sorry for being so vague. What I have is a custom user control that i wish to render more than once. For example: depending on how much data I have I wish it to render it. So if I had 5 objects, the XAML might appear as<x:Usercontrol 1><x:Usercontrol 2><x:Usercontrol 3><x:Usercontrol 4><x:Usercontrol 5>each with the corresponding data.I am going to look into these data templates to see if its what I am looking for.
puttputt
I am probably using UserControls in the complete wrong way, these DataTemplates look like what I should be using.
puttputt
ItemsControl and DataTemplates sounds like what you need.
Will
+3  A: 

I assume you're already using data templates and you're asking how to loop through the objects created to represent your data from the DataTemplate. Am I right? In order to do that you need to use the VisualTreeHelper.

Gustavo Cavalcanti