WPF DataGrid Column Width Auto and Scrollbar

前端 未结 2 1683
抹茶落季
抹茶落季 2021-02-07 06:28

I have a DataGrid with many columns.

I want Width=\"Auto\" with scrollbar showing everything if window narrower than all columns. If window wider I want co

2条回答
  •  庸人自扰
    2021-02-07 07:05

    In XAML set DataGrid ColumnWidth="Auto"

    In UserControl constructor add

    dataGrid.Loaded += (s, e) => { // Column widths
        dataGrid.Columns.AsParallel().ForEach(column => {
            column.MinWidth = column.ActualWidth;
            column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
        });
    };
    

    Using this with a custom DataGrid and works great.

提交回复
热议问题