C# Silverlight-如何将文本框添加到代码隐藏中的堆栈面板
这只是一个让它工作的测试应用程序 我的主页绑定到我的viewmodel,其中包含创建文本框的代码C# Silverlight-如何将文本框添加到代码隐藏中的堆栈面板,c#,silverlight-5.0,stackpanel,C#,Silverlight 5.0,Stackpanel,这只是一个让它工作的测试应用程序 我的主页绑定到我的viewmodel,其中包含创建文本框的代码 private void Generate(object obj) { StackSG = new StackPanel(); StackSGName = new StackPanel(); int st = 10; for (int i = 0; i < st; i++) { T
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);
}
}
这是我的xaml
<StackPanel x:Name="StackSG" Grid.Row="1" Grid.Column="0"/>
<StackPanel x:Name="StackSGName" Grid.Row="1" Grid.Column="1"/>
在这里,我尝试添加文本框
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);
}
}
private void生成(object obj)
{
StackSG=新的StackPanel();
StackSGName=new StackPanel();
int st=10;
对于(int i=0;i
它运行时没有错误,但不会在我的文本框中添加广告。也许这可以帮助您: xaml 视图模型:
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;
}
}
公共类视图模型:INotifyPropertyChanged
{
公共视图模型()
{
StackSG=新列表();
StackSGName=新列表();
生成(空);
}
私有IEnumerable stackSG;
公共IEnumerable StackSG
{
获取{return stackSG;}
设置
{
stackSG=值;
OnNotifyPropertyChanged(“StackSG”);
}
}
私有IEnumerable stackSGName;
公共IEnumerable StackSGName
{
获取{return stackSGName;}
设置
{
stackSGName=值;
OnNotifyPropertyChanged(“StackSGName”);
}
}
公共事件属性更改事件处理程序属性更改;
NotifyPropertyChanged上的公共无效(字符串propName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新PropertyChangedEventArgs(propName));
}
}
专用void生成(对象obj)
{
IList StackSGTmp=新列表();
IList StackSGNameTmp=新列表();
int st=10;
对于(int i=0;i
您是否检查了父网格的行和列的宽度和高度?另外,您是否为stackpanel设置了一个静态高度,以确保文本框可以显示?按照我需要的方式100%工作。谢谢你的帮助!
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;
}
}