gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:38:01 24 4
gpt4 key购买 nike

编辑:

好吧,有点解决了,但感觉很脏:

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 finds all children by type, source for it can be found here

我的目标是在列布局中显示 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>

最佳答案

你能不能打个电话Measure()FlowDocumentReader ,传递给它无限 Size ,然后检查 FlowDocumentReader.DesiredSize属性(property)?

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

关于wpf - 如何确定内容的宽度或容器的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4627847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com