public void AutoResizeColumns()
        {
            if (Items.Count == 0 || !Created)
            {
                return;
            }

            var itemHeight  = Items[0].Bounds.Height;
            var total       = 0;
            var totalWidths = 0.0f;

            for (int i = 0; i < Columns.Count; i++)
            {
                var header = Columns[i] as ColumnHeader;
                if (columnDescs[i].Width == 0.0f)
                {
                    header.Width = itemHeight;
                    total       += header.Width;
                }
                totalWidths += columnDescs[i].Width;
            }

            Debug.Assert(Utils.IsNearlyEqual(totalWidths, 1.0f));

            var remaining = ClientRectangle.Width - total;

            for (int i = 0; i < Columns.Count; i++)
            {
                var header = Columns[i] as ColumnHeader;
                if (columnDescs[i].Width != 0.0f)
                {
                    header.Width = (int)Math.Floor(remaining * columnDescs[i].Width);
                }
            }
        }
Exemple #2
0
        void UpdateColumnWidths(TreeView treeView)
        {
            var propIdx     = GetPropertyIndex(treeView.Parent);
            var prop        = properties[propIdx];
            var columnDescs = prop.columns;

            var itemHeight  = treeView.GetCellArea(new TreePath(new[] { 0 }), treeView.Columns[0]).Height;
            var total       = 0;
            var totalWidths = 0.0f;

            for (int i = 0; i < treeView.Columns.Length; i++)
            {
                var column = treeView.Columns[i];

                // Leave the last column as autosize to avoid going larger than the allocation.
                column.Sizing = i == treeView.Columns.Length - 1 ? TreeViewColumnSizing.Autosize : TreeViewColumnSizing.Fixed;

                if (columnDescs[i].Width == 0.0f)
                {
                    // Dont update width if not needed, this can make the "SizeAllocated"
                    // event go in an infinite loop.
                    if (column.FixedWidth != itemHeight)
                    {
                        column.FixedWidth = itemHeight;
                    }

                    total += treeView.Columns[i].FixedWidth;
                }
                totalWidths += columnDescs[i].Width;
            }

            Debug.Assert(Utils.IsNearlyEqual(totalWidths, 1.0f));

            var remaining = treeView.Allocation.Width - total;

            for (int i = 0; i < treeView.Columns.Length - 1; i++)
            {
                if (columnDescs[i].Width != 0.0f)
                {
                    var column   = treeView.Columns[i];
                    var newWidth = (int)Math.Floor(remaining * columnDescs[i].Width);

                    // Dont update width if not needed, this can make the "SizeAllocated"
                    // event go in an infinite loop.
                    if (column.Width != newWidth)
                    {
                        column.FixedWidth = newWidth;
                    }
                }
            }

            treeView.QueueDraw();
        }