Size GetPreferredEditorSize(Size maxSize, Size cellContentSize, HorizontalAlignment alignment, float indent) { if (!_ownPanel.Excel.CanEditOverflow || string.IsNullOrEmpty(Editor.Text)) { return(cellContentSize); } // 支持文本溢出单元格 Size realSize = MeasureHelper.MeasureText( Editor.Text, Editor.FontFamily, Editor.FontSize, Editor.FontStretch, Editor.FontStyle, Editor.FontWeight, maxSize, true, null, _ownPanel.Excel.UseLayoutRounding, _ownPanel.Excel.ZoomFactor); Size size = MeasureHelper.ConvertTextSizeToExcelCellSize(realSize, _ownPanel.Excel.ZoomFactor); // 多出字符'T'的宽度,不再测量 size.Width += Editor.FontSize; //string text = "T"; //Size size2 = CalcStringSize(new Size(2147483647.0, 2147483647.0), false, text); //size.Width += size2.Width; double width = Math.Min(maxSize.Width, cellContentSize.Width); if (((alignment == HorizontalAlignment.Left) || (alignment == HorizontalAlignment.Right)) && (width < (size.Width + indent))) { size.Width += indent; } return(new Size(Math.Max(width, size.Width), Math.Max(cellContentSize.Height, size.Height))); }
CellOverflowLayout BuildCellOverflowLayoutModelForRight(Cell bindingCell, int rowIndex, CellOverflowLayoutModel result, bool buildForCenter, int deadColumnIndex, object textFormattingMode, bool useLayoutRounding) { ICellsSupport dataContext = Viewport.GetDataContext(); int index = bindingCell.Column.Index; if (index > deadColumnIndex) { Column column = dataContext.Columns[index]; CellLayoutModel cellLayoutModel = Viewport.GetCellLayoutModel(); Column column2 = dataContext.Columns[index - 1]; int num2 = column2.Index; if ((cellLayoutModel != null) && (cellLayoutModel.FindCell(rowIndex, num2) != null)) { return(null); } Cell cachedCell = Viewport.CellCache.GetCachedCell(rowIndex, num2); if ((cachedCell != null) && !string.IsNullOrEmpty(cachedCell.Text)) { return(null); } float zoomFactor = Viewport.Excel.ZoomFactor; Size textSize = MeasureHelper.MeasureTextInCell(bindingCell, new Size(double.PositiveInfinity, double.PositiveInfinity), (double)zoomFactor, null, textFormattingMode, useLayoutRounding); double width = MeasureHelper.ConvertTextSizeToExcelCellSize(textSize, (double)zoomFactor).Width; double num5 = column.ActualWidth * zoomFactor; if (buildForCenter) { width /= 2.0; num5 /= 2.0; } double num6 = num5; if (num6 < width) { CellOverflowLayout layout2 = new CellOverflowLayout(column.Index, num6) { StartingColumn = column.Index }; for (int i = index - 1; (i >= 0) && (i >= deadColumnIndex); i--) { column2 = dataContext.Columns[i]; num2 = column2.Index; if ((cellLayoutModel != null) && (cellLayoutModel.FindCell(rowIndex, num2) != null)) { layout2.BackgroundWidth = num6; layout2.StartingColumn = dataContext.Columns[i + 1].Index; break; } if (((cachedCell != null) && (result != null)) && result.Contains(column2.Index)) { layout2.BackgroundWidth = num6; layout2.StartingColumn = dataContext.Columns[i + 1].Index; break; } cachedCell = Viewport.CellCache.GetCachedCell(rowIndex, num2); if ((cachedCell != null) && !string.IsNullOrEmpty(cachedCell.Text)) { layout2.BackgroundWidth = num6; layout2.StartingColumn = dataContext.Columns[i + 1].Index; break; } num6 += column2.ActualWidth * zoomFactor; if (((num6 >= width) || (i == 0)) || (i <= deadColumnIndex)) { layout2.BackgroundWidth = num6; layout2.StartingColumn = column2.Index; break; } } if (layout2.StartingColumn != column.Index) { layout2.ContentWidth = MeasureHelper.ConvertTextSizeToExcelCellSize(textSize, (double)zoomFactor).Width; layout2.LeftBackgroundWidth = num6; return(layout2); } } } return(null); }
CellOverflowLayoutModel BuildCellOverflowLayoutModel(int rowIndex) { if (!Viewport.Excel.CanCellOverflow) { return(null); } ColumnLayoutModel viewportColumnLayoutModel = Viewport.Excel.GetViewportColumnLayoutModel(Viewport.ColumnViewportIndex); if (viewportColumnLayoutModel == null) { return(null); } object textFormattingMode = null; bool useLayoutRounding = Viewport.Excel.UseLayoutRounding; SpanGraph cachedSpanGraph = Viewport.CachedSpanGraph; CellOverflowLayoutModel result = new CellOverflowLayoutModel(); CellOverflowLayout layout = BuildHeadingCellOverflowLayoutModel(rowIndex, viewportColumnLayoutModel, textFormattingMode, useLayoutRounding); result.HeadingOverflowlayout = layout; for (int i = 0; i < viewportColumnLayoutModel.Count; i++) { Cell cachedCell; CellOverflowLayout layout3; CellOverflowLayout layout5; ColumnLayout layout2 = viewportColumnLayoutModel[i]; if (layout2.Width > 0.0) { cachedCell = Viewport.CellCache.GetCachedCell(rowIndex, layout2.Column); if ((((cachedCell != null) && !string.IsNullOrEmpty(cachedCell.Text)) && (!cachedCell.ActualWordWrap && !cachedCell.ActualShrinkToFit)) && (cachedSpanGraph.GetState(rowIndex, layout2.Column) == 0)) { switch (cachedCell.ToHorizontalAlignment()) { case HorizontalAlignment.Left: case HorizontalAlignment.Stretch: { int deadColumnIndex = Enumerable.Last <ColumnLayout>((IEnumerable <ColumnLayout>)viewportColumnLayoutModel).Column + 1; CellOverflowLayout item = BuildCellOverflowLayoutModelForLeft(cachedCell, rowIndex, false, deadColumnIndex, textFormattingMode, useLayoutRounding); if (item != null) { result.Add(item); int index = viewportColumnLayoutModel.IndexOf(viewportColumnLayoutModel.FindColumn(item.EndingColumn)); if (index > -1) { i = index; } } break; } case HorizontalAlignment.Center: { layout3 = new CellOverflowLayout(layout2.Column, 0.0); int num3 = Enumerable.Last <ColumnLayout>((IEnumerable <ColumnLayout>)viewportColumnLayoutModel).Column + 1; CellOverflowLayout layout4 = BuildCellOverflowLayoutModelForLeft(cachedCell, rowIndex, true, num3, textFormattingMode, useLayoutRounding); num3 = viewportColumnLayoutModel[0].Column - 1; layout5 = BuildCellOverflowLayoutModelForRight(cachedCell, rowIndex, result, true, num3, textFormattingMode, useLayoutRounding); if (layout4 == null) { goto Label_01C1; } layout3.EndingColumn = layout4.EndingColumn; layout3.BackgroundWidth += layout4.BackgroundWidth; layout3.RightBackgroundWidth = layout4.RightBackgroundWidth; goto Label_01E0; } case HorizontalAlignment.Right: { int num6 = viewportColumnLayoutModel[0].Column - 1; CellOverflowLayout layout7 = BuildCellOverflowLayoutModelForRight(cachedCell, rowIndex, result, false, num6, textFormattingMode, useLayoutRounding); if (layout7 != null) { result.Add(layout7); } break; } } } } continue; Label_01C1: layout3.BackgroundWidth += layout2.Width / 2.0; Label_01E0: if (layout5 != null) { layout3.StartingColumn = layout5.StartingColumn; layout3.BackgroundWidth += layout5.BackgroundWidth; layout3.LeftBackgroundWidth = layout5.LeftBackgroundWidth; } else { layout3.BackgroundWidth += layout2.Width / 2.0; } if (layout3.BackgroundWidth > layout2.Width) { Size textSize = MeasureHelper.MeasureTextInCell(cachedCell, new Size(double.PositiveInfinity, double.PositiveInfinity), (double)Viewport.Excel.ZoomFactor, null, textFormattingMode, useLayoutRounding); layout3.ContentWidth = MeasureHelper.ConvertTextSizeToExcelCellSize(textSize, (double)Viewport.Excel.ZoomFactor).Width; result.Add(layout3); } } result.TrailingOverflowlayout = BuildTrailingCellOverflowLayoutModel(rowIndex, viewportColumnLayoutModel, result, textFormattingMode, useLayoutRounding); if (((result.Count <= 0) && (result.HeadingOverflowlayout == null)) && (result.TrailingOverflowlayout == null)) { return(null); } return(result); }
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; } } } }