Search code examples
c#wpf.net-coregrid

How to add grid columns to a specific row in WPF .Net Core 3.1 programatically?


I'm starting to develop application on WPF, so I'm completely new to the project.Don't like XAML, and want to do everything programmatically so here is the question I got two rows, first one is with Ribbon button and for the second one I want to add columns but only for the second row.

When I add the columns on the second row I get columns on the first one also and I don't want that.

Please act like you don't notice that hardcoded int for row count and column count.

public static class GridCustomClass
{
        static GridLength gridLength = new GridLength(0, GridUnitType.Auto);
        static GridLength gridLengthColumns = new GridLength(0, GridUnitType.Auto);
        static GridLength gridLengthColumnsStar = new GridLength(0, GridUnitType.Star);

        public static void AddFirstGridRows(Grid grid)
        {
            int rowsCount = 2;

            for (int rows = 0; rows < rowsCount; rows++)
            {
                RowDefinition rowDefinition = new RowDefinition();
                rowDefinition.Height = gridLength;
                grid.RowDefinitions.Add(rowDefinition);
            }
        }

        public static Grid AddGridColumnsForLeftPane(Grid grid)
        {
            for (int i = 0; i <3; i++)
            {
                ColumnDefinition columnDefinition = new ColumnDefinition();
                switch(i)
                {
                    case 1:
                        columnDefinition.Width = gridLengthColumnsStar;
                        break;
                    default:
                        columnDefinition.Width = gridLengthColumns;
                        break;
                }
                columnDefinition.SetValue(Grid.RowProperty, 1);
                grid.ColumnDefinitions.Add(columnDefinition);
            }
            return grid;
        }
}

public partial class ApplicationSettings : Window
{
        Grid myGrid = new Grid();
        Ribbon ribbon;
        RibbonTab ribbonTab;
        RibbonGroup ribbonGroup;

        public ApplicationSettings()
        {
            InitializeComponent();

            GridCustomClass.AddFirstGridRows(myGrid);
            GridCustomClass.AddGridColumnsForLeftPane(myGrid);
            ribbon = new Ribbon();
            //Grid.SetRow(GridCustomClass.AddGridColumnsForLeftPane(myGrid), 1);
            Button btn = new Button();
           
            btn.SetValue(Grid.ColumnProperty, 0);
            btn.SetValue(Grid.RowProperty,1);

            //Grid.SetRow(btn, 1);

            Grid.SetRow(ribbon, 0);
        
            myGrid.Children.Add(ribbon);
            myGrid.Children.Add(btn);

            settingsWindow.Content = myGrid;
        }
 }

I will be very happy if someone shows me where I go wrong.


Solution

  • One option is to nest grids. Declare a first grid with only one column and two rows, then add Ribbon in row 0. Add a second grid in row 1 and set the number of columns as you wish