<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> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>用Line控件在文字下方添加下划线
时间: 2024-03-05 20:51:43 浏览: 142
你可以在 `DataTemplate` 中添加一个 `Line` 控件来实现下划线效果。以下是修改后的代码示例:
```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>
<StackPanel>
<TextBlock Text="{Binding}" Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}"/>
<Line X1="0" Y1="0" X2="{Binding ActualWidth, ElementName=tb}" Y2="0" Stroke="Teal" StrokeThickness="1"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
```
这段 XAML 代码会在每个 `TextBlock` 下面添加一条宽度与 `TextBlock` 相同的下划线,下划线颜色为 Teal,线条粗细为 1。其中 `ElementName` 属性指向了 `TextBlock` 控件的名称,以获取其实际宽度来设置下划线的长度。
阅读全文