设置数据网格中自动生成列的最大自动生成宽度
我有一个 DataGrid,我将其绑定到由 SQL 查询填充的 DataTable。我希望该网格的列能够自动生成,宽度最多可达一定数量的像素,但如果用户希望其更宽,仍然可以扩展。目前,数据网格上的 ColumnWidth 属性设置为 SizeToHeader (无论如何,它似乎并不像描述的那样工作,因为它仍然根据单元格内容调整大小)。
有没有办法设置最大生成宽度?
设置 MaxColumnWidth 可防止用户将列大小调整为大于该宽度。
我还尝试挂钩 AutoGenerateColumns,但由于行尚未加载,ActualWidth 属性仅代表我设置的 MinWidth。一旦数据网格完成加载其初始行集,是否会触发一个事件?
I have a DataGrid that I'm binding to DataTable that got filled by a SQL query. I would like the columns of this grid to be auto-generated with a width of up to a certain number of pixels, but still be expandable by the user if they want it to be wider. Currently the ColumnWidth property on the datagrid is set to SizeToHeader (which doesn't seem to work as described anyway, since it's still sizing to cell content).
Is there any way to set a max generation width?
Setting MaxColumnWidth prevents the user from resizing the column larger than that width.
I've also tried hooking into AutoGeneratedColumns, but since the rows aren't loaded yet the ActualWidth properties just represent the MinWidth I've set. Is there an event that fires once the datagrid finishes loading its initial set of rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迭代列集合并获取每列的宽度。如果宽度大于阈值,请将其设置为最大宽度。您可能希望在控件初始化后执行此操作。
本文可能会有所帮助:http://support.microsoft.com/kb/812422
类似于这可能有效:
Iterate over the column collection and get the width for each column. If the width is greater than the threshold set it to your maximum width. You would want to do this after the control has initialized.
This article may be helpful: http://support.microsoft.com/kb/812422
Something like this may work:
根据文档,在单个 DataGridColumn 上设置的 Width 属性优先于 ColumnWidth 属性。您也许能够在 AutoGenerateColumns 事件中迭代 DataGridColumns 并设置单独的宽度并让它们保持不变。
From the documentation the Width property set on an individual DataGridColumn takes precedence over the ColumnWidth property. You might be able to iterate over the DataGridColumns in the AutoGeneratedColumns event and set individual Widths and have them stick.
我需要稍微修改 rtalbot 的代码才能使其适用于我的 WPF/.net 4.5 项目...实际宽度和数据网格长度:
I needed to modify rtalbot's code slightly to get this to work for my WPF/.net 4.5 project ... actualwidth and datagridlength: