设置数据网格中自动生成列的最大自动生成宽度

发布于 2024-12-02 15:21:24 字数 345 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-小熊_ 2024-12-09 15:21:24

迭代列集合并获取每列的宽度。如果宽度大于阈值,请将其设置为最大宽度。您可能希望在控件初始化后执行此操作。

本文可能会有所帮助:http://support.microsoft.com/kb/812422

类似于这可能有效:

foreach(var c in grid.Columns)
{
    var width = c.Width;
    if(width > threshold)
    {
        c.Width = threshold;
    }
}

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:

foreach(var c in grid.Columns)
{
    var width = c.Width;
    if(width > threshold)
    {
        c.Width = threshold;
    }
}
浪漫之都 2024-12-09 15:21:24

根据文档,在单个 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.

热情消退 2024-12-09 15:21:24

我需要稍微修改 rtalbot 的代码才能使其适用于我的 WPF/.net 4.5 项目...实际宽度和数据网格长度:

        // fix extra wide columns
        foreach (var c in dgMain.Columns)
        {
            var width = c.ActualWidth;
            if (width > 200)
            {
                c.Width = new DataGridLength(200);
            }
        }

I needed to modify rtalbot's code slightly to get this to work for my WPF/.net 4.5 project ... actualwidth and datagridlength:

        // fix extra wide columns
        foreach (var c in dgMain.Columns)
        {
            var width = c.ActualWidth;
            if (width > 200)
            {
                c.Width = new DataGridLength(200);
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文