2013-09-21 35 views
0

我有arraylistist.let說它包含15個元素。我將這些添加到堆棧面板。我需要每行添加3個元素。我的代碼在下面。我得到水平或verrtical.Let知道如何做到這一點。在wpf中獲取新行

MainWindow w; 
    public ShopCart(MainWindow m,ArrayList _list) 
    { 

     InitializeComponent(); 
     w = m; 


     int i = 1; 

     foreach (string cartitems in _list) 
     { 

       mystackpanel.Orientation = Orientation.Horizontal; 
       mystackpanel.Margin.Left.Equals(150); 
       Label lbl = new Label(); 
       lbl.Name = "Label" + i; 
       lbl.Height = 30; 
       lbl.Width = 200; 
       lbl.Margin.Left.Equals(150); 
       //lbl.Margin.Top.Equals(150); 
       lbl.Content = cartitems.ToString(); 
       mystackpanel.Children.Add(lbl); 
       i++; 

       int str = mystackpanel.Children.Count; 
       MessageBox.Show(Convert.ToString(str)); 
       if (str%3 == 0) 
       { 
        Button btndelete = new Button(); 
        btndelete.Content = "Delete"; 
        btndelete.Width = 120; 
        btndelete.Height = 35; 
        mystackpanel.Children.Add(btndelete); 

        mystackpanel.Margin.Top.Equals(500); 


       } 








     } 

回答

1

下面是一些示例代碼(假設你已經有按鈕的列表,並且將外疊面板添加到您的主控),你可以試試,你可能需要改變一些東西按你的需要:

  List<Button> buttons = new List<Button>(); 
      StackPanel panel = new StackPanel(); 
      panel.Orientation = Orientation.Horizontal; 
      int count = 0; 
      StackPanel innerPanel = new StackPanel(); 
      innerPanel.Orientation = Orientation.Vertical; 

      foreach (Button button in buttons) 
      { 
       innerPanel.Children.Add(button); 
       ++count; 
       if (count % 3 == 0 && count != 0) 
       { 
        panel.Children.Add(innerPanel); 
        innerPanel = new StackPanel(); 
        innerPanel.Orientation = Orientation.Vertical; 
       } 
      } 

      if (panel.Children.Contains(innerPanel) == false) 
      { 
       panel.Children.Add(innerPanel); 
      } 

雖然在我看來,最好的辦法是有一個帶有n * n行和列的網格,並將你的按鈕添加到相應的行,列。