0

I am dynamically adding several textboxes to my grid in my code behind. I would like to be able to capture what the user enters into those textboxes.

I'm not quiet sure how to do this as the name of the dynamically added textbox is not available when I try to add it in my codebehind.

I want to create a querybuilder tool. This is very rudementary but basically I want to add multiple comboboxes, textboxes and buttons.

What I'm trying to accomplish

webdad3
  • 8,893
  • 30
  • 121
  • 223
  • 1
    `I am dynamically adding several textboxes to my grid in my code behind` - Wrong approach. Learn MVVM. Use an `ItemsControl`. WPF is not winforms. Post a screenshot of what you need and I can tell you the proper way to do it in WPF. – Federico Berasategui May 15 '13 at 17:19
  • Ok do you have a good tutorial on the ItemsControl? – webdad3 May 15 '13 at 17:23
  • 1
    Maybe [this](http://stackoverflow.com/a/16367662/643085) can help you. Still, It's a little bit different from what you're describing here. Post a screenshot of what you need. – Federico Berasategui May 15 '13 at 17:25
  • done - I added a VERY rudimentary screenshot – webdad3 May 15 '13 at 17:29
  • I'm doing some thing like this in c# project...in my idea maybe you can search these textBoxes by their names and types in the your grid children.I saw your code befor you edite your own post, maybe you could have defined your stackpanel globally and then search your textboxes through it – Fuad May 15 '13 at 17:49
  • 1
    @Fuad See my answer. That's the CORRECT approach in WPF. – Federico Berasategui May 15 '13 at 18:00
  • Yah..I got it...Using "{Bindings}" are usual and Correct way in WPF...nice answer friend.Thanks for awaring me :) – Fuad May 15 '13 at 18:19

3 Answers3

2

First of all, you must leave behind the traditional mentality of manipulating UI elements in code and Embrace MVVM

<Window x:Class="MiscSamples.QueryBuilderSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MiscSamples"
        Title="QueryBuilderSample" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <ComboBox ItemsSource="{Binding Operators}" 
                              SelectedItem="{Binding Operator}"/>

                    <TextBox Text="{Binding Value}" Grid.Column="1"/>

                    <Button Content="Add" Grid.Column="2"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

Code behind:

 public partial class QueryBuilderSample : Window
    {
        public List<QueryCriteria> Criterias { get; set; } 

        public QueryBuilderSample()
        {
            InitializeComponent();

            DataContext = Criterias = Enumerable.Range(0, 10).Select(x => new QueryCriteria()).ToList();
        }
    }

ViewModel:

public class QueryCriteria
{
    public List<Operators> Operators
    {
        get
        {
            return Enum.GetValues(typeof(Operators))
                       .Cast<Operators>()
                       .ToList();
        }
    }

    public Operators Operator { get; set; }
    public string Value { get; set; }
}

public enum Operators
{
    Equals,
    Contains,
    GreaterThan,
    SmallerThan,
}

Result:

enter image description here

  • Notice that I'm not doing a single line of code to create / manipulate UI elements. Everything is done via DataBinding.
  • Simple code. No complex event handling stuff or anything like that.
  • Declarative Code. Just Simple, Simple Properties and INotifyPropertyChanged. That's the default approach to EVERYTHING in WPF.
Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • thank you very much for this code. It has been too long since I did any MVVM so I appreciate the re-direction – webdad3 May 15 '13 at 22:20
0

When you insert a text box, keep a reference to it in some object. A dictionary could be a good choice. That way, you can get that reference later, and from that reference you can read its Text property.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
0

@jeff V you can simply capture the textbox text by using the name your assigning those textboxes.. you are using tb1,tb2 as textboxes name...so you can easily get the values using

Haji
  • 1,999
  • 1
  • 15
  • 21