在wpf textblock控件中包装时保留缩进
我用属性TextWrapping=“Wrap”设置了一个WPF textblock 当我在开始时传入一个带有制表符的长字符串(在我的例子中是vbTab)时,我希望包装能够遵守这一点,并保持字符串的包装部分缩进。 例如,而不是: [vbTab]这很长 包裹 我想要 [vbTab]这很长 [vbTab]并已包装 最理想的是多个标签,等等 [编辑-其他详细信息] 因为textblock的大小是可变的,并且包含多行具有不同缩进量的文本,所以我不能只留一个边距或手动拆分字符串并添加制表符在wpf textblock控件中包装时保留缩进,wpf,vb.net,xaml,textblock,word-wrap,Wpf,Vb.net,Xaml,Textblock,Word Wrap,我用属性TextWrapping=“Wrap”设置了一个WPF textblock 当我在开始时传入一个带有制表符的长字符串(在我的例子中是vbTab)时,我希望包装能够遵守这一点,并保持字符串的包装部分缩进。 例如,而不是: [vbTab]这很长 包裹 我想要 [vbTab]这很长 [vbTab]并已包装 最理想的是多个标签,等等 [编辑-其他详细信息] 因为textblock的大小是可变的,并且包含多行具有不同缩进量的文本,所以我不能只留一个边距或手动拆分字符串并添加制表符 基本上,我想让它
基本上,我想让它像处理段落一样处理文本行,在换行时保持缩进。基于您的想法,我能够想出这个解决方案 我将把每行开头的所有制表符转换为每行0.5英寸的边距,并在段落中添加相同的文本,并将计算出的边距应用于相同的文本 TextBlock不适用于基本的文本内联线,如粗体运行、内联ui容器等。在TextBlock中添加段落更复杂,因此我基于FlowDocument提出了解决方案 结果 下面的示例使用
FlowDocumentScrollViewer
或RichTextBox
或FlowDocumentReader
或普通FlowDocument
我已经使用附加属性创建了解决方案,因此您可以将其附加到所提到的任何文档,甚至可以为文档添加您自己的主机。您只需将IndentationProvider.Text
设置为所需的主机
XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:l="clr-namespace:PreservingIndentationDemo"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<sys:String x:Key="longString"
xml:space="preserve">	this is really long and wrapped
		another line this is also really long and wrapped
	one more line this is also really long and wrapped
another line this is also really long and wrapped
		another line this is also really long and wrapped
</sys:String>
</Window.Resources>
<Grid>
<FlowDocumentScrollViewer l:IndentationProvider.Text="{StaticResource longString}" />
<!--<RichTextBox l:TextToParaHelper.Text="{StaticResource longString}" IsReadOnly="True"/>-->
<!--<FlowDocumentReader l:TextToParaHelper.Text="{StaticResource longString}" />-->
<!--<FlowDocument l:TextToParaHelper.Text="{StaticResource longString}" />-->
</Grid>
</Window>
演示
试试看基于你的想法,我能想出这个解决方案 我将把每行开头的所有制表符转换为每行0.5英寸的边距,并在段落中添加相同的文本,并将计算出的边距应用于相同的文本 TextBlock不适用于基本的文本内联线,如粗体运行、内联ui容器等。在TextBlock中添加段落更复杂,因此我基于FlowDocument提出了解决方案 结果 下面的示例使用
FlowDocumentScrollViewer
或RichTextBox
或FlowDocumentReader
或普通FlowDocument
我已经使用附加属性创建了解决方案,因此您可以将其附加到所提到的任何文档,甚至可以为文档添加您自己的主机。您只需将IndentationProvider.Text
设置为所需的主机
XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:l="clr-namespace:PreservingIndentationDemo"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<sys:String x:Key="longString"
xml:space="preserve">	this is really long and wrapped
		another line this is also really long and wrapped
	one more line this is also really long and wrapped
another line this is also really long and wrapped
		another line this is also really long and wrapped
</sys:String>
</Window.Resources>
<Grid>
<FlowDocumentScrollViewer l:IndentationProvider.Text="{StaticResource longString}" />
<!--<RichTextBox l:TextToParaHelper.Text="{StaticResource longString}" IsReadOnly="True"/>-->
<!--<FlowDocumentReader l:TextToParaHelper.Text="{StaticResource longString}" />-->
<!--<FlowDocument l:TextToParaHelper.Text="{StaticResource longString}" />-->
</Grid>
</Window>
演示
如果字符串中确实有一个制表符,可以在文本块上加上
xml:space=“preserve”
,否则,如果您只想让所有内容都有一个剩余的空间,也许只要在字符串上加上一个左边的边距就可以实现您想要的视觉效果?问题不在于保留制表符,它们显示得很好——当文本环绕多行时,它保持行缩进。此时,包装文本的第二行(等)再次靠在左边缘。您可能需要在此处留边距,制表符可能不可行。是的,我支持@pushpraj,听起来你只是想去掉制表符,让一个边距来完成你想要的格式。但我不一定想要文本块中每一行的缩进量相同,边距会迫使我这样做如果字符串中实际上有一个制表符,可以在你的文本块上打xml:space=“preserve”
,否则,如果您只想让所有内容都有一个左空格,那么它上的左边距就可以实现您想要的视觉效果?问题不在于保留选项卡,它们显示得很好-而是在文本换行时保持行缩进。此时,包装文本的第二行(等)再次靠在左边缘。您可能需要在此处留边距,制表符可能不可行。是的,我支持@pushpraj,听起来你只是想去掉标签页,让一个边距来完成你想要的格式。但是我不一定想要文本块中每行的缩进量相同,边距会迫使我这么做。我会说谢谢你的挑战和想法,我也学会了一个新的技巧。我要说谢谢你的挑战和想法,我也学会了一个新的技巧。