我正在使用列表框来显示带有数据模板的撤消重做列表:
<ListBox x:Name="actionList"
Height="150"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
MouseMove="ListBoxMouseMove"
ScrollViewer.VerticalScrollBarVisibility="Visible"
SelectionMode="Extended"
Style="{StaticResource CustomListBoxStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Width="235"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
FontSize="11"
Text="{Binding DisplayText}"
TextWrapping="Wrap" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
它按预期工作,但对于长撤消重做字符串,会发生换行,但它与行的第一个字符对齐。我们希望它缩进一点以清楚地识别两个列表项。图示如下:
我们怎样才能达到同样的效果。
最佳答案
“更简单”的方法可能涉及使用 Paragraph 对象来表示文本。
段落本身支持缩进,属性如 TextIndent(控制首行缩进,您可以将其设置为负值)或 Margin(设置整行的边距段落,但尊重首行缩进)。
<ListBox.ItemTemplate>
<DataTemplate>
// IsHitTestVisible is set to false to avoid FlowDocument's built-in text selection
// from disrupting the regular ListBox mouse selection behavior
<Grid IsHitTestVisible="False">
<FlowDocumentScrollViewer ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<FlowDocument FontSize="12"
FontFamily="Calibri"
Foreground="Black"
PagePadding="0">
<Paragraph TextIndent="-10"
Margin="10,0,0,0">
<Run Text="{Binding ., Mode=OneWay}" />
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
关于wpf - 在 WPF textBlock 中用缩进换行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30752389/