static Size GetTextSize(Cell cell) { Size maxSize = new Size(double.PositiveInfinity, double.PositiveInfinity); if (cell.ActualWordWrap) { CellRange range = cell.Worksheet.GetSpanCell(cell.Row.Index, cell.Column.Index, cell.SheetArea); if ((range != null) && ((range.Row < cell.Row.Index) || (range.RowCount > 1))) { return(new Size(0.0, 0.0)); } double width = 0.0; if (range == null) { width = cell.Worksheet.GetColumnWidth(cell.Column.Index, cell.SheetArea); } else { for (int i = 0; i < range.ColumnCount; i++) { width += cell.Worksheet.GetColumnWidth(cell.Column.Index + i, cell.SheetArea); } } width *= cell.Worksheet.ZoomFactor; maxSize = MeasureHelper.ConvertExcelCellSizeToTextSize(new Size(width, double.PositiveInfinity), 1.0); } return(MeasureHelper.MeasureTextInCell(cell, maxSize, (double)cell.Worksheet.ZoomFactor, cell.ActualFontFamily, null, false)); }
void ApplyStyle() { HorizontalAlignment horAlignment = BindingCell.ToHorizontalAlignment(); if (_tb.HorizontalAlignment != horAlignment) { _tb.HorizontalAlignment = horAlignment; } // uno绘制Right位置错误,慎用TextAlignment! //Windows.UI.Xaml.TextAlignment textAlignment; //switch (horAlignment) //{ // case HorizontalAlignment.Center: // textAlignment = Windows.UI.Xaml.TextAlignment.Center; // break; // case HorizontalAlignment.Right: // textAlignment = Windows.UI.Xaml.TextAlignment.Right; // break; // default: // textAlignment = Windows.UI.Xaml.TextAlignment.Left; // break; //} //if (_tb.TextAlignment != textAlignment) // _tb.TextAlignment = textAlignment; VerticalAlignment verAlignment; switch (BindingCell.ActualVerticalAlignment) { case CellVerticalAlignment.Top: verAlignment = VerticalAlignment.Top; break; case CellVerticalAlignment.Bottom: verAlignment = VerticalAlignment.Bottom; break; default: verAlignment = VerticalAlignment.Center; break; } if (_tb.VerticalAlignment != verAlignment) { _tb.VerticalAlignment = verAlignment; } var foreground = BindingCell.ActualForeground; if (foreground == null) { // 默认黑色 if (_tb.ReadLocalValue(TextBlock.ForegroundProperty) != DependencyProperty.UnsetValue) { _tb.ClearValue(TextBlock.ForegroundProperty); } } else if (foreground != _tb.Foreground) { _tb.Foreground = foreground; } var fontStyle = BindingCell.ActualFontStyle; if (_tb.FontStyle != fontStyle) { _tb.FontStyle = fontStyle; } var fontWeight = BindingCell.ActualFontWeight; if (_tb.FontWeight.Weight != fontWeight.Weight) { _tb.FontWeight = fontWeight; } var fontFamily = BindingCell.ActualFontFamily; if (fontFamily != null && _tb.FontFamily.Source != fontFamily.Source) { _tb.FontFamily = fontFamily; } bool wrap = BindingCell.ActualWordWrap; TextWrapping textWrap = wrap ? TextWrapping.Wrap : TextWrapping.NoWrap; if (_tb.TextWrapping != textWrap) { _tb.TextWrapping = textWrap; } double fontSize = BindingCell.ActualFontSize * ZoomFactor; double fitZoom = -1; if (!wrap && BindingCell.ActualShrinkToFit) { // 自动缩小字体适应单元格宽度 double textWidth = MeasureHelper.MeasureText( _tb.Text, _tb.FontFamily, fontSize, _tb.FontStretch, _tb.FontStyle, _tb.FontWeight, new Size(double.PositiveInfinity, double.PositiveInfinity), false, null, _tb.UseLayoutRounding, ZoomFactor).Width; double cellWidth = BindingCell.Worksheet.GetActualColumnWidth(BindingCell.Column.Index, BindingCell.ColumnSpan, BindingCell.SheetArea) * ZoomFactor; cellWidth = MeasureHelper.ConvertExcelCellSizeToTextSize(new Size(cellWidth, double.PositiveInfinity), ZoomFactor).Width; cellWidth = Math.Max((double)0.0, (double)(cellWidth - BindingCell.ActualTextIndent * ZoomFactor)); if (cellWidth < textWidth) { fitZoom = cellWidth / textWidth; } } if (fitZoom > 0) { fontSize *= fitZoom; } if (_tb.FontSize != fontSize) { _tb.FontSize = fontSize; } // TextBlock设置Padding时,若Padding左右之和大于Measure时给的Width,uno莫名报错,布局混乱,不易发现! Thickness margin = new Thickness(); var indent = BindingCell.ActualTextIndent * ZoomFactor; if (indent > 0 && _tb.HorizontalAlignment != HorizontalAlignment.Center) { if (_tb.HorizontalAlignment == HorizontalAlignment.Right) { margin.Right += indent; } else { margin.Left += indent; } } if (_tb.Margin != margin) { _tb.Margin = margin; } // 未用到 //var fontStretch = BindingCell.ActualFontStretch; //if (_tb.FontStretch != fontStretch) // _tb.FontStretch = fontStretch; if (BindingCell.ActualUnderline) { Underline underline = new Underline(); Run run = new Run(); run.Text = _tb.Text; underline.Inlines.Add(run); _tb.Inlines.Clear(); _tb.Inlines.Add(underline); _lastUnderline = true; } else if (_lastUnderline) { string str = _tb.Text; _tb.Inlines.Clear(); _tb.Text = str; } if (BindingCell.ActualStrikethrough) { foreach (UIElement element in (_tb.Parent as Panel).Children) { if (element is StrikethroughView) { StrikethroughView view = element as StrikethroughView; if (view.LineContainer != null) { foreach (var line in view.LineContainer.Children.OfType <Line>()) { line.Stroke = _tb.Foreground; } } break; } } } }