puttputt
puttputt

Reputation: 1269

Loop through UserControl in WPF

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?

Upvotes: 1

Views: 2583

Answers (2)

Gus
Gus

Reputation: 10659

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.

Upvotes: 4

user1228
user1228

Reputation:

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.

Upvotes: 3

Related Questions