2016-05-23 60 views
1

我的TextBlock中的文本被绑定到我的代码中的元素。但是,当我第一次打开窗口时,文本块完全是空的。当我添加文本时,我需要ScrollViewer允许我向下滚动文本,或者在添加更多文本时自动向下滚动。使用MVVM,所以没有任何代码会是理想的。带有绑定文本的WPF文本块不会滚动

<StackPanel Grid.Column="0" Grid.Row="1" Margin="0 10"> 
    <Label Style="{StaticResource LabelStyle}">Output</Label> 
    <ScrollViewer VerticalScrollBarVisibility="Visible" Height="100"> 
     <TextBlock ScrollViewer.CanContentScroll="True" Height="100" VerticalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding Path=ProcessingOutput}"/> 
    </ScrollViewer> 
</StackPanel> 

我该如何做到这一点?有没有办法更新ScrollViewer,以便看到更多的文字超出了我在TextBlock中可以看到的范围,并且允许用户向下滚动,或者允许我设置自动滚动功能,以便在通过绑定添加文本时自动向下滚动?

提前致谢!如果从TextBlock

删除Height="100"使其滚动时文本更改其他答案建议使用ScrollViwer.ScrollToBottom()方法,例如

回答

0

滚动条将工作像这样:

<ScrollViewer Name="scroll" 
       VerticalScrollBarVisibility="Visible" 
       Height="100"> 
    <TextBlock ScrollViewer.CanContentScroll="True" 
       VerticalAlignment="Stretch" 
       TextWrapping="Wrap" 
       Text="{Binding Path=ProcessingOutput, NotifyOnTargetUpdated=True}" 
       TargetUpdated="Txt_OnTargetUpdated"> 
     </TextBlock> 
</ScrollViewer> 
private void Txt_OnTargetUpdated(object sender, DataTransferEventArgs e) 
{ 
    scroll.ScrollToBottom(); 
} 
+0

哇,我搜索了一整天了这一点,从工作的TextBlock中采取的高度!谢谢!! – HaydenThrash