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

发布于 2024-10-11 01:59:22 字数 2435 浏览 7 评论 0原文

编辑:

好吧,它已经解决了,但感觉很脏:

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 在大多数情况下会滚动到文档末尾: https://i.sstatic.net/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>

EDIT:

Ok, it's kind of solved, but it feels dirty:

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;
    }
}

I looked in Snoop, and it seems one of the ContainerVisual objects stores the correct width in its DescendantBounds property. Its parent is PageVisual (this class is internal, though, so the string compare with SystemType.FullName or GetType().ToString() was used which probably sucks)

Note: FindVisualChildren finds all children by type, source for it can be found here


My goal is to display the entire contents of a FlowDocument (that is, without paginating) in a column layout. It would have a fixed height, but the width would depend on the contents of the FlowDocument.

My problem is: FlowDocumentReader does not automatically resize to the contents of the FlowDocument. As you see in my XAML below, FlowDocumentReader.Width is 5000 units (just a large number that can accommodate most documents) -- when I make it Auto, it just clips to the width of the ScrollViewer and paginates my stuff!

Is there a proper way of solving this problem?

I also made a screenshot of what this looks like now, but the ScrollViewer scrolls past the end of the document in most cases: https://i.sstatic.net/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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高冷爸爸 2024-10-18 01:59:22

您可以在 FlowDocumentReader 上调用 Measure(),向其传递一个无限的 Size,然后检查 FlowDocumentReader.DesiredSize > 财产?

flowDocumentReader.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var desiredSize = flowDocumentReader.DesiredSize;

Could you just call Measure() on the FlowDocumentReader, passing it an infinite Size, then check the FlowDocumentReader.DesiredSize property?

flowDocumentReader.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var desiredSize = flowDocumentReader.DesiredSize;
小耗子 2024-10-18 01:59:22

我不清楚你所说的“在列布局中”是什么意思。您的意思是多列和水平滚动条吗?或者你的意思是单列和垂直滚动条?

如果您想要单列和垂直滚动条,您可能需要查看 FlowDocumentScrollViewer”。我发现它比 FlowDocumentReader 更容易使用。

I'm not clear on what you mean by "in a column layout". Do you mean multiple columns, and a horizontal scrollbar? Or do you mean a single column, and a vertical scrollbar?

If you want a single column and a vertical scrollbar, you might want to look at FlowDocumentScrollViewer instead. I've found it much easier to use than FlowDocumentReader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文