If I have this structure:
public class Parent { public string Name{get; set;} public List<Child> Childs {get; set;} } public class Child { public string Name{get; set;} public int Age {get; set;} public bool Married {get; set;} } public class ParentFactory { public List<Parent> Parents {get; set;} public ParentFactory() { Child child1 = new Child() {Name="Peter", Age=10, Married=true}; Child child2 = new Child() {Name="Mary", Age=9, Married=false}; Child child3 = new Child() {Name="Becky", Age=12, Married=true}; Parent parent1 = new Parent(){Name="Adam", Childs = new List<Child>(){child1, child2}}; Parent parent2 = new Parent(){Name="Kevin", Childs = new List<Child>(){child3}}; Parents = new List<Parent>(){parent1, parent2}; } }
I want to bind the ParentFactory parentFactory = new ParentFactory()
object ParentFactory parentFactory = new ParentFactory()
to the ItemsControl:
<DockPanel> <ItemsControl ItemsSource="{Binding Parents}"> </ItemsControl> </DockPanel> <Window.Resources> <DataTemplate DataType="{x:Type Parent}"> <StackPanel Margin="2,2,2,1"> <Expander Header="{Binding Name}"> <ItemsControl ItemsSource="{Binding Childs}" /> </Expander> </StackPanel> </DataTemplate> <DataTemplate DataType="{x:Type Child}"> <StackPanel> <TextBox Grid.Column="0" Text="{Binding Name}" /> <TextBox Grid.Column="1" Text="{Binding Age}"/> <CheckBox Grid.Column="2" IsChecked="{Binding Married}"/> </StackPanel> </DataTemplate> </Window.Resources>
There are two types of controls in Stackpanel: TextBox and CheckBox. However, I want them to be more dynamic: if the value is logical, then use the checkbox and still use the text box. This means that I do not need to define a TextBox or Checkbox control inside the StackPanel due to the many attributes in my Child class. It would be possible, and if so, how can I achieve them?
source share