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

时间:2013-07-30 09:16:14

标签: c# silverlight-5.0 stackpanel

这只是一个让它运作的测试应用程序

我的主页绑定到我的viewmodel,其中包含创建文本框的代码

这是我的xaml

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

然后我有一个按钮来实际生成文本框。以下是我如何定义堆栈面板并创建文本框。

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

在这里,我尝试添加文本框

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

它运行时没有错误,但不会显示我的文本框。

1 个答案:

答案 0 :(得分:0)

也许这可以帮助你一点点:

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>
代码背后的代码:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        this.DataContext = new 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;
    }
}