GETTING NEW LINE IN WPF

Pravesh Singh

Total Post:412

Points:2888
Posted by  Pravesh Singh
WPF 
WPF
 1086  View(s)
Ratings:
Rate this:

I am having arraylist.let say it contains 15 elements. I am adding these to stack panel. I need to add 3 elements per line. my code is below. I am getting either horizontal or verrtical.Let me know how to do this.

    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. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Getting new line in wpf

    Hi Pravesh,


    Here is some sample code(assuming you already have a list of buttons, and will add outer stack panel to your main control) you can try, you may need to change few things as per your need:

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

                }

    Although in my opinion the best way will be to have a Grid with n*n row and columns and add your buttons to respective row, columns.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!