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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:51:02  来源:igfitidea点击:

How to get a WPF TextBlock to scroll where the Text property is set asynchronously?

c#.netwpfasynchronoustextblock

提问by MalcomTucker

I have a TextBlock, wrapped in a ScrollViewer, and the Textproperty of the TextBlockis set with the result of a Task. The scrollbars of the TextBlockdo 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

ScrollViewercalculates it's scrollbars based on dimensions of child controls.

ScrollViewer根据子控件的尺寸计算它的滚动条。

Thus, remove Heightproperty from your TextBlockand ScrollBars should work as expected

因此,Height从您的TextBlock和 ScrollBars 中删除属性应该按预期工作