views:

529

answers:

2

Hi. I'm doing some stuff in WPF.

I have a ComboBox with many Types. After selecting a concrete type, I want to be able to Load a particular UserControl (with many TextBoxes etc.) respecting the type in a defined region on my WPF Window.

How to do that? Is there anything like LoadControl() and load it into a PlaceHolder in ASP.NET? And what's the best way of doing this?

Thanks.

+2  A: 

You should create a DataTemplate for each type. See Different item template for each item in a WPF List for more information.

Julien Poulin
Well Ok I can define many DataTemplate and with selector decide, for what Item which Template to show. Thank you!
PaN1C_Showt1Me
But there is another problem. That ComboBox is located directly in the template, because it's a part of that item. That means that while adding, you choose an item from that combobox and the item changes itself to another DataTemplate. But i want to preserve the state of that ComboBox. But after loading a new DataTemplate it will be lost. Any idea?
PaN1C_Showt1Me
It look weird that the ComboBox is part of the DataTemplate. You say in your question that a UserControl is to be displayed in a 'defined region' of the window. It should be in that defined region's resources that you should define that different templates for each type. Also, you don't have to 'load' any UserControl, it will be done automatically for you. If there is something I didn't get, please provide some code to better illustrate your problem.
Julien Poulin
Well i was thinking of putting the ComboBox inside, so that for every Item user could dynamically change its Type. The result would be that this concrete item changed the Type.
PaN1C_Showt1Me
OK I won't put the ComboBox inside the DataTemplate.. It's a bit nonsense. Then, everything's clear. Thank you!
PaN1C_Showt1Me
+1  A: 

What about:

object o = Activator.CreateInstance(type); 
myUserControl.PubliclyExposedContainer.Content = o;

Not really sure why you'd go to the trouble of creating a DataTemplate for each type.

Anderson Imes