C# 如何让 WPF TextBlock 在异步设置 Text 属性的位置滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15027622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a WPF TextBlock to scroll where the Text property is set asynchronously?
提问by MalcomTucker
I have a TextBlock
, wrapped in a ScrollViewer
, and the Text
property of the TextBlock
is set with the result of a Task. The scrollbars of the TextBlock
do not adjust to the size of the text returned by the task.
我有一个TextBlock
, 包裹在 a 中ScrollViewer
,并且 的Text
属性TextBlock
是用任务的结果设置的。的滚动条TextBlock
不会根据任务返回的文本大小进行调整。
Any ideas?
有任何想法吗?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="500"/>
</Grid.ColumnDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto"
Height="177"
Width="500"
HorizontalScrollBarVisibility="Disabled">
<TextBlock Height="177"
Text="Extracted Xml"
Width="504"
HorizontalAlignment="Stretch"
TextWrapping="Wrap" />
</ScrollViewer>
</Grid>
采纳答案by Nogard
ScrollViewer
calculates it's scrollbars based on dimensions of child controls.
ScrollViewer
根据子控件的尺寸计算它的滚动条。
Thus, remove Height
property from your TextBlock
and ScrollBars should work as expected
因此,Height
从您的TextBlock
和 ScrollBars 中删除属性应该按预期工作