简体   繁体   English

Silverlight-如何在后面的代码中将文本框添加到堆栈面板

[英]Silverlight - How do I add textboxes to the stack panel in the code behind

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 这是我的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. 它可以正常运行,但不会在我的文本框中显示广告。

Maybe this can help you a little: 也许这可以对您有所帮助:

xaml a

<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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM