locked
WPF automatically creates text boxes upon binding RRS feed

  • Question

  • Is there any control available in WPF which upon binding creates text boxes automatically? For example if i have a object like below then it should create text boxes for each property

    public class Person
    {
       public string Name {get;set;}
       
       public int Age {get;set;}
       
       public string Address {get;set}
    }

    Tuesday, June 3, 2014 10:03 PM

Answers

  • No there is no such control available. You can create a Custom Control and have such option.

    Else you can have a DataGrid with AutoGenerateColumns property as true. Or an ItemsControl and have three textboxes in DataTemplate. But in both controls you would need a collection for ItemsSource property.

            <ItemsControl>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
    
                            <TextBox Text="{Binding Path=Name, Mode=TwoWay}" Grid.Column="0"/>
                            <TextBox Text="{Binding Path=Age, Mode=TwoWay}" Grid.Column="1"/>
                            <TextBox Text="{Binding Path=Address, Mode=TwoWay}" Grid.Column="2"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor


    Wednesday, June 4, 2014 7:15 AM
  • Hi, you can try PropertyGrid:

    http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid

    For example:

        <Grid>
            <xctk:PropertyGrid Name="propertyGrid" />
        </Grid>
    propertyGrid.SelectedObject = new Person();

    You will see


    Wednesday, June 4, 2014 7:36 AM
  • The closest you get is the DataGrid control. If you set its ItemsSource property to a collection of Person objects, it will create a column for each property in the class automatically. If you double click on a row, you will enter edit mode and be able to change the property values of the object that belongs to the current row in TextBoxes.

    <DataGrid x:Name="dGrid" />

    List<Person> persons = new List<Person>();
    persons.Add(new Person() { Name = "name...", Age = 30, Address = "address..." });
    dGrid.ItemsSource = persons;
    


    Wednesday, June 4, 2014 8:41 AM

All replies

  • No there is no such control available. You can create a Custom Control and have such option.

    Else you can have a DataGrid with AutoGenerateColumns property as true. Or an ItemsControl and have three textboxes in DataTemplate. But in both controls you would need a collection for ItemsSource property.

            <ItemsControl>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
    
                            <TextBox Text="{Binding Path=Name, Mode=TwoWay}" Grid.Column="0"/>
                            <TextBox Text="{Binding Path=Age, Mode=TwoWay}" Grid.Column="1"/>
                            <TextBox Text="{Binding Path=Address, Mode=TwoWay}" Grid.Column="2"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor


    Wednesday, June 4, 2014 7:15 AM
  • Hi, you can try PropertyGrid:

    http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid

    For example:

        <Grid>
            <xctk:PropertyGrid Name="propertyGrid" />
        </Grid>
    propertyGrid.SelectedObject = new Person();

    You will see


    Wednesday, June 4, 2014 7:36 AM
  • The closest you get is the DataGrid control. If you set its ItemsSource property to a collection of Person objects, it will create a column for each property in the class automatically. If you double click on a row, you will enter edit mode and be able to change the property values of the object that belongs to the current row in TextBoxes.

    <DataGrid x:Name="dGrid" />

    List<Person> persons = new List<Person>();
    persons.Add(new Person() { Name = "name...", Age = 30, Address = "address..." });
    dGrid.ItemsSource = persons;
    


    Wednesday, June 4, 2014 8:41 AM