none
如何获取richetextbox里面,各种flowdocument元素的宽度高度 RRS feed

  • 问题

  • 尝试各种方法无果,获取的宽高始终为0,把元素插入richtextbox,取它的宽高也不行

    有没有什么好的办法呢

    2016年5月21日 0:31

答案

  • 您好 wpf萌新,

    如果您要获取Height, 那么Width是需要首先指定的。同样如果您要获取Width,那么您需要首先指定Height。您可以试试下面的代码。我已经测试过了,是可行的。

    private double GetFlowDocumentHeight(double width)
    {
        var b = new Image
        {
            Source = new BitmapImage(new Uri("pack://application:,,,/" + "office.jpg", UriKind.RelativeOrAbsolute)),
            Width = 30,
            Height = 30,
            Tag = "pack://application:,,,/" + "office.jpg"
        };
    
        Figure pf = new Figure();
        pf.HorizontalAnchor = FigureHorizontalAnchor.PageLeft;
        pf.Margin = new Thickness(1, 0, 0, 0);
    
        Paragraph pTemp = new Paragraph();
    
        pTemp.TextAlignment = TextAlignment.Left;
        pTemp.Inlines.Add(new Run("aaaaaa") { Background = Brushes.Red });
        pTemp.Inlines.Add(new InlineUIContainer(b) { BaselineAlignment = BaselineAlignment.Top });
    
        pf.Blocks.Add(pTemp);
    
        
        var flowDoc = new FlowDocument(new Paragraph(pf));
    
        var richtextbox = new RichTextBox { Width = width, Document = flowDoc };
        var viewbox = new Viewbox { Child = richtextbox };
    
        viewbox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        viewbox.Arrange(new Rect(viewbox.DesiredSize));
    
        var size = new Size() { Height = viewbox.ActualHeight, Width = viewbox.ActualWidth };
    
        return size.Height;
    }

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年5月23日 6:35