在wpf中获得新的一行

时间:2013-09-21 12:06:09

标签: wpf

我有arraylist.let说它包含15个元素。我将这些添加到堆栈面板。我需要每行添加3个元素。我的代码如下。我要么是水平的还是狡猾的。让我知道如何做到这一点。

    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 个答案:

答案 0 :(得分: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行和列的网格,并将你的按钮添加到各自的行,列。