自动调整 WPF TreeListView 中的列大小

发布于 2024-10-06 12:42:59 字数 905 浏览 0 评论 0原文

我正在尝试使用以下代码自动调整 WPF TreeListView (http://www.codeproject.com/KB/WPF/wpf_treelistview_control.aspx) 的列大小:

    public void AutoResizeColumns()
    {
        GridView gv = this.View as GridView;

        if (gv != null)
        {
            foreach (GridViewColumn gvc in gv.Columns)
            {

                if (double.IsNaN(gvc.Width))
                    gvc.Width = gvc.ActualWidth;

                gvc.Width = double.NaN;
            }
        }
    }

但是当我调整它的大小时,列宽度不考虑边距行和单词被截断了大约 10px,然后如果我双击该列,它将调整大小而不会截断单词。

我也尝试过,但没有成功:

    public void AutoResizeColumns()
    {
        GridView gv = this.View as GridView;

        if (gv != null)
        {
            foreach (GridViewColumn gvc in gv.Columns)
            {

                gvc.Width = gvc.ActualWidth + 10;
            }
        }
    }

有人知道如何解决这个问题吗?

I am trying to auto resize the columns of WPF TreeListView (http://www.codeproject.com/KB/WPF/wpf_treelistview_control.aspx) using this code:

    public void AutoResizeColumns()
    {
        GridView gv = this.View as GridView;

        if (gv != null)
        {
            foreach (GridViewColumn gvc in gv.Columns)
            {

                if (double.IsNaN(gvc.Width))
                    gvc.Width = gvc.ActualWidth;

                gvc.Width = double.NaN;
            }
        }
    }

But when I resize it, the column width is not accounting for the margin of the row and the words are cut off by like 10px and then if I double click the column, it will resize without cutting off the words.

I have also tried this with no luck:

    public void AutoResizeColumns()
    {
        GridView gv = this.View as GridView;

        if (gv != null)
        {
            foreach (GridViewColumn gvc in gv.Columns)
            {

                gvc.Width = gvc.ActualWidth + 10;
            }
        }
    }

Does anyone know how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

末蓝 2024-10-13 12:43:00

经过几个小时的尝试解决这个问题,我终于明白了。列宽度被设置为 ActualWidth,它小于其应有的宽度,因此,如果我将列宽度设置为 double.MaxValue,那么当其设置为 double.NaN 时,它将调整大小为“真实”实际宽度。

这是代码:

public void AutoResizeColumns()
{
    GridView gv = this.View as GridView;

    if (gv != null)
    {
        foreach (GridViewColumn gvc in gv.Columns)
        {
            // Set width to highest possible value
            gvc.Width = double.MaxValue;

            // Set to NaN to get the "real" actual width
            gvc.Width = double.NaN;
        }
    }
}

After hours of trying to figure this out, I finally got it. The column Width is being set to ActualWidth which is less then its supposed to be, so if I set the column Width to double.MaxValue then when its set to double.NaN it will resize to the "real" actual width.

Here's the code:

public void AutoResizeColumns()
{
    GridView gv = this.View as GridView;

    if (gv != null)
    {
        foreach (GridViewColumn gvc in gv.Columns)
        {
            // Set width to highest possible value
            gvc.Width = double.MaxValue;

            // Set to NaN to get the "real" actual width
            gvc.Width = double.NaN;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文