How to make DataGrid column stretch from C# code?

前端 未结 2 1391
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 18:54

I\'m creating the columns of a WPF DataGrid in my C# code. I want one of the columns to stretch to the width of the DataGrid automatically. In XAML

2条回答
  •  萌比男神i
    2021-01-17 19:22

    You're actually using something called a DataGridLength there.

    Try something like this:

    Grid.ColumnDefinitions[0].Width = new DataGridLength(0.2, DataGridLengthUnitType.Star);
    Grid.ColumnDefinitions[1].Width = new DataGridLength(0.8, DataGridLengthUnitType.Star);
    

    That will split the remaining space 80% / 20% between these two columns.

提交回复
热议问题