<ItemsControl x:Class="HDSProject.Control.CustomFont" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HDSProject.Control" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="itemCtrl" ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}" FontSize="20" Foreground="Teal" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}"> <TextBlock Text="{Binding}" /> <Border.Style> <Style TargetType="Border"> <Setter Property="BorderThickness" Value="0"/> <Style.Triggers> <DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True"> <Setter Property="BorderThickness" Value="0 0 0 1"/> </DataTrigger> </Style.Triggers> </Style> </Border.Style> <Border.BorderBrush> <SolidColorBrush Color="{Binding Foreground.Color, ElementName=itemCtrl}" /> </Border.BorderBrush> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>我希望Title2中如果带回车,显示的文字可以跟着分成多行展示,且行与行之间的间距可以设置
时间: 2024-02-14 15:08:41 浏览: 110
要实现Title2中带回车的多行展示,可以使用TextBlock来代替Border,并将TextBlock的Text属性设置为绑定的值,并将其TextWrapping属性设置为Wrap,如下所示:
```xaml
<TextBlock Text="{Binding}" TextWrapping="Wrap" />
```
此外,要设置行与行之间的间距,可以将TextBlock的Margin属性设置为一个带有Top或Bottom值的Thickness,例如:
```xaml
<TextBlock Text="{Binding}" TextWrapping="Wrap" Margin="0 5 0 5" />
```
这会在每一行的顶部和底部添加5个像素的间距。最终修改后的代码如下所示:
```xaml
<ItemsControl x:Class="HDSProject.Control.CustomFont"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HDSProject.Control"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
x:Name="itemCtrl"
ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}"
FontSize="20"
Foreground="Teal"
>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}">
<TextBlock Text="{Binding}" TextWrapping="Wrap" Margin="0 5 0 5" />
<Border.Style>
<Style TargetType="Border">
<Setter Property="BorderThickness" Value="0"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True">
<Setter Property="BorderThickness" Value="0 0 0 1"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Border.BorderBrush>
<SolidColorBrush Color="{Binding Foreground.Color, ElementName=itemCtrl}" />
</Border.BorderBrush>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
```
阅读全文