如何确定内容的宽度或容器的大小

编辑: 好的,它已经解决了,但感觉很脏:
foreach (ContainerVisual cv in SurfaceNYTR.Helpers.VFTreeHelper.FindVisualChildren<ContainerVisual>(flowDocReader))
{
    if (cv.Parent.DependencyObjectType.SystemType.FullName == "MS.Internal.PtsHost.PageVisual")
    {
      flowDocReader.Width = cv.DescendantBounds.Width;
    }
}
我查看了Snoop,似乎其中一个ContainerVisual对象在其DescendantBounds属性中存储了正确的宽度。它的父级是PageVisual(这个类是内部的,所以字符串与SystemType.FullName或GetType()比较。使用的ToString()可能很糟糕) 注意:FindVisualChildren按类型查找所有子项,可在此处找到源代码 我的目标是在列布局中显示FlowDocument的全部内容(即没有分页)。它将具有固定的高度,但宽度将取决于FlowDocument的内容。 我的问题是:FlowDocumentReader不会自动调整大小到FlowDocument的内容。正如您在下面的XAML中看到的那样,FlowDocumentReader.Width是5000个单位(只是一个可以容纳大多数文档的大数字) - 当我将其设置为Auto时,它只是剪辑到ScrollViewer的宽度并分页我的东西! 有没有正确的方法来解决这个问题? 我还制作了现在看起来像的截图,但在大多数情况下,ScrollViewer滚动到文档的末尾:http://i.stack.imgur.com/3FSRl.png
<ScrollViewer x:Name="scrollViewer" 
              HorizontalScrollBarVisibility="Visible" 
              VerticalScrollBarVisibility="Disabled"
              >
        <FlowDocumentReader x:Name="flowDocReader" 
                            ClipToBounds="False" 
                            Width="5000"
                            >
            <FlowDocument   x:Name="flowDoc"
                            Foreground="#FF404040" 
                            ColumnRuleWidth="2" 
                            ColumnGap="40" 
                            ColumnRuleBrush="#FF404040" 
                            IsHyphenationEnabled="True" 
                            IsOptimalParagraphEnabled="True" 
                            ColumnWidth="150">

                <Paragraph>
                    Lorem ipsum dolor sit amet, ...etc...
                </Paragraph>
                <Paragraph>
                    Lorem ipsum dolor sit amet, ...etc...
                </Paragraph>
                <Paragraph>
                    Lorem ipsum dolor sit amet, ...etc...
                </Paragraph>
            </FlowDocument>
        </FlowDocumentReader>
</ScrollViewer>
    
已邀请:
你可以在
FlowDocumentReader
上打电话给
Measure()
,把它传给无限
Size
,然后查看
FlowDocumentReader.DesiredSize
属性吗?
flowDocumentReader.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var desiredSize = flowDocumentReader.DesiredSize;
    
我不清楚你在“列布局”中的意思。你的意思是多列和水平滚动条?或者你的意思是一个列和一个垂直滚动条? 如果您需要单个列和垂直滚动条,则可能需要查看FlowDocumentScrollViewer。我发现它比FlowDocumentReader更容易使用。     

要回复问题请先登录注册