I need to dynamically add the controls to a window. For example for a comboBox, I need to map with the Custom Framework which will map the Lookup to that comboBox…
So whenever the combobox is mapped with lookup, I need to Add a (+) Button to the right side of the comboBox.. This (+) button is used to open a respective lookup form to add an item to the comboBox.
For that I need to do the following..
- In Mapper I need to create instance for Button Label with (+),
- I need to create a Panel., ie., Container for holding both the Controls (ComboBox,Button)
- For replacing Combobox with that created panel, I need to remove the Combobox from the Parent of Combobox and add panel to that control.
How can we do this in WPF? ie. dynamically replacing controls in Code behind.
Thanks in advance, Dinesh
I would suggest using MVVM pattern with separation of View and ViewModel. To solve your issue you can use DataTemplates along with a DataTemplateSelector. Basically you should prepare different Data templates and depends on internal state of applicaiton / a particular ViewModel switch Views (XAML layouts).
1) Define all templates in resources:
2) Specify selector in Control’s presenter:
3) Define selector in View’s code behind:
EDIT: Dynamically build controls
find children controls, check whether they are of specific type and then remove/update
control.Parentproperty(parent as Panel).Children.Remove()(parent as Panel).Children.Add()