Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7808561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:10:14+00:00 2026-06-02T03:10:14+00:00

If I have this structure: public class Parent { public string Name{get; set;} public

  • 0

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 object ParentFactory parentFactory = new ParentFactory() to 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>

In the Stackpanel, there are two types of controls: TextBox and CheckBox. However, I want them to be more dynamic: if the value is a boolean then use a Checkbox and else use a Textbox. That means I dont need to define the control either TextBox or Checkbox inside the StackPanel due to a numerous attributes in my Child class. Would it be possible, and if yes, how can I achieve them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:10:15+00:00Added an answer on June 2, 2026 at 3:10 am

    I have done a solution from what i understood from your question. Please have a look at it.
    The sample is based on the DataTrigger and you can change the logics to Converter.

    <Window x:Class="StackAnswers.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:t="clr-namespace:StackAnswers">
    
    
    <Window.Resources>
        <DataTemplate DataType="{x:Type t:Parent}">
            <StackPanel Margin="2,2,2,1">
                <Expander Header="{Binding Name}">
                    <ItemsControl ItemsSource="{Binding Childs}" />
                </Expander>
            </StackPanel>
        </DataTemplate>
        <DataTemplate DataType="{x:Type t:Child}">
            <StackPanel>
                <TextBlock Text="{Binding Name}"></TextBlock>
                <TextBox Grid.Column="0"
                         Text="{Binding Name}">
                    <TextBox.Style>
                        <Style TargetType="{x:Type TextBox}">
                            <Setter Property="Visibility"
                                    Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Married}" Value="false">
                                    <Setter Property="Visibility"
                                            Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
                <TextBox Grid.Column="1"
                         Text="{Binding Age}">
                    <TextBox.Style>
                        <Style TargetType="{x:Type TextBox}">
                        <Setter Property="Visibility"
                                Value="Collapsed" />
                        <Style.Triggers>
                                <DataTrigger Binding="{Binding Married}"
                                         Value="false">
                                <Setter Property="Visibility"
                                        Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                        </TextBox.Style>
                </TextBox>
                <CheckBox Grid.Column="2"
                          IsChecked="{Binding Married}" Content="Married">
                    <CheckBox.Style>
                        <Style TargetType="{x:Type CheckBox}">
                            <Setter Property="Visibility"
                                    Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Married}" Value="True">
                                    <Setter Property="Visibility"
                                            Value="Visible"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </CheckBox.Style>
                </CheckBox>
            </StackPanel>
        </DataTemplate>
    
    
    </Window.Resources>
    <DockPanel>
        <ItemsControl ItemsSource="{Binding Parents}">
        </ItemsControl>
    </DockPanel>
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have this structure: public class Parent { public string Name{get; set;} public
I have this simple structure: 1 parent, and two different childs. public class Parent{}
I have a object structure like this: public class Entity { IList<Relationship> Relationships{get;set;} }
I have this structure of classes: public class L3Message { public int Number {
Have the following structure [Serializable] public class Parent { public int x = 5;
Let's say I have this simple structure class FooDefinition { public FooDefinition Parent {
I have following structure: class Employee { public long Id { get; set; }
I have a structure similar to this: class OuterClass{ AnimatorListener sth; public OuterClass(){ sth
Suppose i have this structure of elements: <div class=parent> <div class=something1> <div class=something2> <div
I have the following Object: public class Constraint { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) int id; String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.