2013-07-30 68 views
0

這僅僅是一個測試應用程序,使其能工作Silverlight的 - 如何在代碼中添加文本框到堆棧面板後面

我的炫魅結合我的視圖模型保存代碼來創建文本框

這裏是我的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); 
     } 
    } 

它運行沒有錯誤,但沒有廣告我的文本框中。

+0

你檢查的寬度和高度你父母的網格的行和列?另外,你是否將一個靜態高度放到了堆棧面板上以確保文本框可以出現? – Oliver

回答

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

以我需要的方式100%工作。 Thanx的幫助! – Sl1ver

相關問題