コード例 #1
0
        public void WriteColumnDefinition(string width, double minWidth, double maxWidth)
        {
            GridLength gridLength = GridLength.Auto;

            width = width.Trim();
            if (string.Compare(width, "auto", StringComparison.OrdinalIgnoreCase) == 0)
            {
                tableWidth += 100; // give this column a minimum size at least
                gridLength  = new GridLength(0, GridUnitType.Auto);
            }
            else if (width == "*")
            {
                tableWidth += 100; // give this column a minimum size at least
                gridLength  = new GridLength(0, GridUnitType.Star);
            }
            else
            {
                double w;
                if (double.TryParse(width, out w))
                {
                    tableWidth += w;
                    gridLength  = new GridLength(w);
                }
                else
                {
                    throw new ArgumentException("width should be 'auto', '*', or a valid number", "width");
                }
            }
            var ext = new ColumnWidthExtensions()
            {
                MinWidth = minWidth, MaxWidth = maxWidth
            };

            current.table.Columns.Add(new TableColumn()
            {
                Width = gridLength, Tag = ext
            });
        }
コード例 #2
0
        private void FixAutoColumns(Table table)
        {
            // 'Auto' sized columns in FlowDocument tables suck.
            // This code fixes that.
            Brush brush = Brushes.Black;

            List <TableColumn> columns = new List <TableColumn>(current.table.Columns);

            if (columns.Count == 0)
            {
                return;
            }
            List <double> maxWidths = new List <double>();

            for (int i = 0; i < columns.Count; i++)
            {
                maxWidths.Add(0);
            }

            foreach (var group in current.table.RowGroups)
            {
                foreach (var row in group.Rows)
                {
                    int i = 0;
                    foreach (var cell in row.Cells)
                    {
                        if (columns[i].Width.IsAuto)
                        {
                            foreach (var block in cell.Blocks)
                            {
                                if (block is Table)
                                {
                                    // can't measure these ones.
                                    return;
                                }
                                TextRange range = new TextRange(block.ContentStart, block.ContentEnd);
                                string    text  = range.Text;
                                if (!string.IsNullOrEmpty(text))
                                {
                                    Typeface      tface = new Typeface(block.FontFamily, block.FontStyle, block.FontWeight, block.FontStretch);
                                    FormattedText ft    = new FormattedText(text, System.Globalization.CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, tface, block.FontSize, brush, this.pixelsPerDip);
                                    maxWidths[i] = Math.Max(maxWidths[i], ft.Width + cell.Padding.Left + cell.Padding.Right);
                                }
                            }
                        }
                        i += cell.ColumnSpan;
                    }
                }
            }

            // fixup 'auto' columns so they have the right width
            int j = 0;

            foreach (var col in current.table.Columns)
            {
                ColumnWidthExtensions ext = col.Tag as ColumnWidthExtensions;
                if (col.Width.IsAuto && ext != null && ext.MinWidth > 0)
                {
                    double w = Math.Max(ext.MinWidth, maxWidths[j] + 10);
                    w         = Math.Min(w, ext.MaxWidth);
                    col.Width = new GridLength(w);
                }
                j++;
            }
        }