Question

This is just a test application to get it working

My mainpage binds to my viewmodel that holds the code to create textboxes

here is my xaml

<StackPanel x:Name="StackSG" Grid.Row="1" Grid.Column="0"/>
<StackPanel x:Name="StackSGName" Grid.Row="1" Grid.Column="1"/>

Then I have a button to actually generate the text boxes. Here is how i defined the stack panels and create the textboxes.

 private StackPanel stackSG;
    public StackPanel StackSG
    {
        get { return stackSG; }
        set { stackSG = value; OnNotifyPropertyChanged("StackSG"); }
    }

    private StackPanel stackSGName;
    public StackPanel StackSGName
    {
        get { return stackSGName; }
        set { stackSGName = value; OnNotifyPropertyChanged("StackSGName"); }
    }

And here I try and add the textboxes

private void Generate(object obj)
    {
        StackSG = new StackPanel();
        StackSGName = new StackPanel();

        int st = 10;
        for (int i = 0; i < st; i++)
        {
            TextBox txtSG = new TextBox();
            txtSG.Name = string.Format("{0}{1}", "Te", i.ToString());
            txtSG.Height = 25;
            txtSG.Text = string.Format("{0}{1}", "Te", i.ToString());
            txtSG.IsReadOnly = true;
            StackSG.Children.Add(txtSG);

            //Add SG name textboxes                        
            TextBox txtSGName = new TextBox();
            txtSGName.Name = string.Format("{0}{1}", "Test", i.ToString());
            txtSGName.Height = 25;
            txtSGName.Text = string.Format("{0}{1}", "Test", i.ToString());
            txtSGName.IsReadOnly = true;
            StackSGName.Children.Add(txtSGName);
        }
    }

It runs without error but doesn't ad my text boxes.

Was it helpful?

Solution

Maybe this can help you a little:

xaml

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <StackPanel x:Name="StackSG" Grid.Row="0" Grid.Column="0">
        <ListBox ItemsSource="{Binding StackSG}"/>
    </StackPanel>
    <StackPanel x:Name="StackSGName" Grid.Row="0" Grid.Column="1" >
        <ListBox ItemsSource="{Binding StackSGName}"/>
    </StackPanel>
</Grid>

code behind:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        this.DataContext = new ViewModel(); 
    }
}

viewmodel:

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        StackSG = new List<TextBox>();
        StackSGName = new List<TextBox>();
        Generate(null);
    }

    private IEnumerable stackSG;
    public IEnumerable StackSG
    {
        get { return stackSG; }
        set
        {
            stackSG = value;
            OnNotifyPropertyChanged("StackSG");
        }
    }

    private IEnumerable stackSGName;
    public IEnumerable StackSGName
    {
        get { return stackSGName; }
        set
        {
            stackSGName = value;
            OnNotifyPropertyChanged("StackSGName");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnNotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    private void Generate(object obj)
    {
        IList<TextBox> StackSGTmp = new List<TextBox>();
        IList<TextBox> StackSGNameTmp = new List<TextBox>();

        int st = 10;
        for (int i = 0; i < st; i++)
        {
            TextBox txtSG = new TextBox();
            txtSG.Name = string.Format("{0}{1}", "Te", i.ToString());
            txtSG.Height = 25;
            txtSG.Text = string.Format("{0}{1}", "Te", i.ToString());
            txtSG.IsReadOnly = true;
            StackSGTmp.Add(txtSG);

            //Add SG name textboxes                        
            TextBox txtSGName = new TextBox();
            txtSGName.Name = string.Format("{0}{1}", "Test", i.ToString());
            txtSGName.Height = 25;
            txtSGName.Text = string.Format("{0}{1}", "Test", i.ToString());
            txtSGName.IsReadOnly = true;
            StackSGNameTmp.Add(txtSGName);
        }

        StackSG = StackSGTmp;
        StackSGName = StackSGNameTmp;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top