wpf 仅带有垂直滚动条的 TextBlock
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18742641/
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
TextBlock with vertical scrollbar only
提问by Alireza Noori
I have a TextBlockwhich may contain a long text so I want to add a vertical scroll bar to it. My initial attempt was to wrap a ScrollVieweraround it. That works but the problem is that when I zoom in, the width is zoomed also. I tried disabling the horizontal scroll bar like this:
我有一个TextBlock可能包含长文本的内容,因此我想向其中添加一个垂直滚动条。我最初的尝试是ScrollViewer围绕它包装一个。那行得通,但问题是当我放大时,宽度也会放大。我尝试像这样禁用水平滚动条:
<ScrollViewer IsTabStop="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<ScrollViewer IsTabStop="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
But it didn't solve the problem. I also tried binding the width:
但这并没有解决问题。我也尝试绑定宽度:
Width="{Binding ElementName=Scroller, Path=ViewportWidth}"
Width="{Binding ElementName=Scroller, Path=ViewportWidth}"
It didn't help either.
它也没有帮助。
So, my question is, how can I add vertical scrollbar to it but have a fixed width and wrapped text for the TextBlockinside? Here's my full code:
所以,我的问题是,如何向其中添加垂直滚动条,但在TextBlock内部具有固定宽度和换行文本?这是我的完整代码:
<ScrollViewer Grid.Row="1" IsTabStop="True" VerticalScrollBarVisibility="Auto">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" TextWrapping="Wrap" TextAlignment="Center"/>
</ScrollViewer>
采纳答案by Sheridan
There are two parts to this answer... the first is to simply use a TextBox:
这个答案有两个部分……第一个是简单地使用 a TextBox:
<TextBox ScrollViewer.VerticalScrollBarVisibility="Visible" Text="Something really
really really really really really really really really long"
Style="{StaticResource TextBlockStyle}" />
The second part is to simply Stylethe TextBoxso that it lookslike a TextBlock:
第二部分是简单Style的,TextBox所以它看起来像一个TextBlock:
<Style x:Key="TextBlockStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="TextWrapping" Value="Wrap" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
Feel free to remove any of these properties if they do not suit your situation.
如果这些属性不适合您的情况,请随意删除它们。
回答by 123 456 789 0
<TextBox HorizontalAlignment="Center"
VerticalAlignment="Top"
TextWrapping="Wrap"
TextAlignment="Center"
VerticalScrollBarVisibility="Auto" Width="300" Style="{StaticResource TextBlockStyle}"/>
You don't need a ScrollViewerwrapped in the TextBox, the TextBoxcontrol has its own ScrollViewer. And you need to define the width of the TextBoxso that the scrollbar will know its fixed width and will wrap the text.
您不需要ScrollViewer包裹在 中TextBox,TextBox控件有自己的ScrollViewer. 并且您需要定义 的宽度,TextBox以便滚动条知道其固定宽度并包裹文本。
Then, you have to style the TextBox to look like a TextBlock
然后,您必须将 TextBox 的样式设置为看起来像 TextBlock
A good reason why this ScrollViewerwon't work according to to Ifeanyi Echeruo from Microsoft, from MSDN
ScrollViewer根据来自 Microsoft 的 Ifeanyi Echeruo,来自MSDN 的说法,这是为什么这不起作用的一个很好的原因
ScrollViewer first asks its content how large it would like to be in the absence of constraints, if the content requires more space than the Viewer has then its time to kick in some ScrollBars
In the absence of constraints TextBlock will always opt to return a size where all text fits on a single line.
A ScrollViewer with ScrollBars will never get a TextBlock to wrap.
However you may be able to come up with a Measure\Arrange combination for a panel of your own that is almost like ScrollViewer but I cant think of any logic that can satify both constraints without explicit knowlege of the behaviour of said children
ScrollViewer 首先询问它的内容在没有约束的情况下它希望有多大,如果内容需要比 Viewer 更多的空间,那么它是时候启动一些 ScrollBars
在没有约束的情况下,TextBlock 将始终选择返回所有文本都适合一行的大小。
带有 ScrollBars 的 ScrollViewer 永远不会得到要包装的 TextBlock。
但是,您可以为自己的面板提出一个 Measure\Arrange 组合,这几乎就像 ScrollViewer,但我想不出任何逻辑可以满足这两个约束,而无需明确了解所述孩子的行为

