Ejemplo n.º 1
0
        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;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        protected override Size ArrangeOverride(Size finalSize)
        {
            if (Column == -1 || finalSize.Width < _textPadding * 2)
            {
                foreach (UIElement elem in Children)
                {
                    elem.Arrange(_rcEmpty);
                }
                return(_szEmpty);
            }

            Rect?nullable = null;

            if (_overflowLayout != null && _overflowLayout.ContentWidth > finalSize.Width)
            {
                double w;
                double left = 0;
                switch (BindingCell.ToHorizontalAlignment())
                {
                case HorizontalAlignment.Left:
                case HorizontalAlignment.Stretch:
                    left = _textPadding;
                    w    = _overflowLayout.RightBackgroundWidth;
                    if (w >= 0.0)
                    {
                        nullable = new Rect(0.0, 0.0, w, finalSize.Height);
                    }
                    break;

                case HorizontalAlignment.Right:
                    left = finalSize.Width - _overflowLayout.LeftBackgroundWidth;
                    w    = _overflowLayout.LeftBackgroundWidth;
                    if (w >= 0.0)
                    {
                        nullable = new Rect(left, 0.0, w, finalSize.Height);
                    }
                    break;

                default:
                    left -= (_overflowLayout.ContentWidth - finalSize.Width) / 2.0;
                    double x = 0.0;
                    if (_overflowLayout.LeftBackgroundWidth > 0.0)
                    {
                        x = (finalSize.Width / 2.0) - _overflowLayout.LeftBackgroundWidth;
                    }

                    w = _overflowLayout.BackgroundWidth;
                    if (w >= 0.0)
                    {
                        nullable = new Rect(x, 0.0, w, finalSize.Height);
                    }
                    break;
                }

                _tb.Arrange(new Rect(left, 0, _overflowLayout.ContentWidth + _textPadding * 2, finalSize.Height));
            }
            else
            {
                // 文字默认边距:4,0,4,0
                _tb.Arrange(new Rect(_textPadding, 0, finalSize.Width - _textPadding * 2, finalSize.Height));
            }

            if ((_cachedClip.HasValue != nullable.HasValue) || (_cachedClip.HasValue && (_cachedClip.Value != nullable.Value)))
            {
                _cachedClip = nullable;
                if (nullable.HasValue)
                {
                    Clip = new RectangleGeometry {
                        Rect = nullable.Value
                    };
                }
                else
                {
                    ClearValue(ClipProperty);
                }
            }

            if (Children.Count > 1)
            {
                Rect rect = new Rect(new Point(), finalSize);
                for (int i = 1; i < Children.Count; i++)
                {
                    ((UIElement)Children[i]).Arrange(rect);
                }
            }
            return(finalSize);
        }