【问题标题】:How to add grid columns to a specific row in WPF .Net Core 3.1 programatically?如何以编程方式将网格列添加到 WPF .Net Core 3.1 中的特定行?
【发布时间】:2020-04-30 07:01:59
【问题描述】:

我开始在 WPF 上开发应用程序,所以我对这个项目完全陌生。不喜欢 XAML,并且想以编程方式完成所有事情,所以这是我得到两行的问题,第一行是 Ribbon按钮,对于第二个,我想添加列,但只针对第二行。

当我在第二行添加列时,我也会在第一行得到列,我不希望这样。

请表现得好像您没有注意到行数和列数的硬编码 int。

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

如果有人告诉我哪里出错了,我会非常高兴。

【问题讨论】:

    标签: c# wpf .net-core grid


    【解决方案1】:

    一种选择是嵌套网格。声明一个只有一列和两行的第一个网格,然后在第 0 行添加 Ribbon。在第 1 行添加第二个网格并根据需要设置列数

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多