WPF DataGrid 列宽自动和滚动条 [英] WPF DataGrid Column Width Auto and Scrollbar

查看:156
本文介绍了WPF DataGrid 列宽自动和滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多列的 DataGrid.

I have a DataGrid with many columns.

如果窗口比所有列窄,我想要 Width="Auto" 带有滚动条显示所有内容.如果窗口更宽,我希望列跨越空白空间,因此没有死空间.

I want Width="Auto" with scrollbar showing everything if window narrower than all columns. If window wider I want columns to span empty space so there is no dead space.

基本上我希望列的最小宽度完全适合内容或标题.如果窗口更宽,则扩展到更大.

Basically I want the column minimum width to fully fit contents or header. And expand to larger if window wider.

推荐答案

在 XAML 中设置 DataGrid ColumnWidth="Auto"

In XAML set DataGrid ColumnWidth="Auto"

在 UserControl 构造函数中添加

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

将此与自定义 DataGrid 结合使用,效果很好.

Using this with a custom DataGrid and works great.

这篇关于WPF DataGrid 列宽自动和滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆